I practice to build a simple rest api using express js. It’s running well but i’m not really sure this follow the MVC architecture.
please give me a feedback to improve my code.
here’s my code
server.js
running my server from here:
const express = require('express');
const cors = require('cors');
const { getAllNotes, postNewNote, updateNoteById, deleteNoteById, notFoundError, getNoteByTitle } = require('./controllers/controllers.js');
const app = express();
const port = 3001;
app.use(express.json());
app.use(cors());
app.get('/', (req, res) => {
res.send({
response: req.query.tes
});
});
app.route('/notes')
.get(getAllNotes)
.post(postNewNote)
.put(updateNoteById)
.delete(deleteNoteById);
app.route('/notes/:title')
.get(getNoteByTitle);
app.route('*')
.all(notFoundError);
app.listen(port, () => console.log(`app listening on http://localhost:${port}`));
and here’s my db connection code.
db.js
const mysql = require('mysql');
const conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'daily_task'
});
conn.connect( err => {
if (err) throw err;
console.log('Success connected to db');
});
module.exports = conn;
controller.js
const { nanoid } = require('nanoid');
const sql = require('../db/db.js');
const getAllNotes = (req, res, next) => {
const getAllQuery = 'SELECT * FROM notes';
sql.query(getAllQuery, (err, result) => {
if (err) {
res.send({
status: 'ERROR',
error: 'Failed to GET notes'
});
next(err);
} else {
res.send({
notes: result
});
}
});
};
const getNoteByTitle = (req, res, next) => {
const getNoteByIdQuery = `SELECT * FROM notes WHERE title LIKE '%${req.params.title}%'`;
sql.query(getNoteByIdQuery, (err, result) => {
if (err) {
res.send({
status: 'ERROR',
error: 'Failed to GET note'
});
next(err);
} else {
res.send({
notes: result
});
}
});
};
const postNewNote = (req, res, next) => {
const id = nanoid(16);
const postNoteQuery = `INSERT INTO notes (id, title, note) VALUES ('${id}', '${req.body.title}', '${req.body.note}')`;
sql.query(postNoteQuery, (err) => {
if (err) {
res.send({
status: 'ERROR',
error: 'Failed to POST notes'
});
next(err);
} else {
res.send({
id: req.body.id,
title: req.body.title,
note: req.body.note,
status: 'ADDED',
});
}
});
};
const updateNoteById = (req, res, next) => {
const updateNoteByIdQuery = `UPDATE notes SET title = '${req.body.title}', note = '${req.body.note}' WHERE id = '${req.body.id}'`;
sql.query(updateNoteByIdQuery, (err) => {
if (err) {
res.send({
status: 'ERROR',
error: 'Failed to UPDATE notes'
});
next(err);
} else {
res.send({
id: req.body.id,
title: req.body.title,
note: req.body.note,
status: 'UPDATED'
});
}
});
};
const deleteNoteById = (req, res, next) => {
const deleteNoteByIdQuery = `DELETE FROM notes WHERE id = '${req.body.id}'`;
sql.query(deleteNoteByIdQuery, (err) => {
if (err) {
res.send({
status: 'ERROR',
error: 'Failed to DELETE notes'
});
next(err);
} else {
res.send({
id: req.body.id,
status: 'DELETED'
});
}
});
};
const notFoundError = (req, res) => {
res.status(404).send({
response: 'Not Found'
});
};
module.exports = {
getAllNotes,
getNoteByTitle,
postNewNote,
updateNoteById,
deleteNoteById,
notFoundError
};
please give me some review for my first build rest api with express js. any input and feedback would be appreciated.