0

I am learning nodejs and I created few API when I have my all functionality in server.js it is working properly. Now when I separated my code in dbConfig.js, server.js, and in ProductController.js. I started receiving 404, but my DB connection is working fine.
server.js:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const conn = require('./connection/dbConfig');
const productRouter = require('./controller/ProductController');
// parse application/json
app.use(bodyParser.json());
//Server listening
app.listen(3000,() =>{
  console.log('Server started on port 3000...');
});

And my ProductController.js:

'user strict';
 const conn = require('../connection/dbConfig');
 const express = require('express');
 const app = express();
 
//show all products
app.get('/getAllProducts',(req, res) => {
  let sql = "SELECT * FROM product";
  let query = conn.query(sql, (err, results) => {
    if(err) throw err;
    res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
  });
});
 
//show single product
app.get('/getProductById/:id',(req, res) => {
  let sql = "SELECT * FROM product WHERE product_id="+req.params.id;
  let query = conn.query(sql, (err, results) => {
    if(err) throw err;
    res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
  });
});
 
//add new product
app.post('/addProduct',(req, res) => {
  let data = {product_name: req.body.product_name, product_price: req.body.product_price};
  let sql = "INSERT INTO product SET ?";
  let query = conn.query(sql, data,(err, results) => {
    if(err) throw err;
    res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
  });
});
1
  • Code 404 error means 'file not found'. You must be doing something wrong with file structure. Can you update the file structure in the question itself to get some idea which file location at which place? Commented Jun 21, 2020 at 12:55

1 Answer 1

1

What is missing on your application is the use of routing. Basically your application currently doesn't know that those routes are defined on ProductController.js because you didn't tell it to know so.

Here is a quick fix for you and a quick example on how to setup routing:

Your main file:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const conn = require('./connection/dbConfig');
const productRouter = require('./controller/ProductController');

// parse application/json
app.use(bodyParser.json());

// NEW LINE: You should tell the application to use the productRouter.
// The first parameter tells the base location of the route and the second tells where it's located on the project
app.use('/products', productRouter);

//Server listening
app.listen(3000,() =>{
  console.log('Server started on port 3000...');
});

Your ProductController.js file:

'use strict';
const conn = require('../connection/dbConfig');
const {Router} = require('express');

const route = new Router();
 
//show all products
route.get('/get',(req, res) => {
  let sql = "SELECT * FROM product";
  let query = conn.query(sql, (err, results) => {
    if(err) throw err;
    res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
  });
});
 
//show single product
route.get('/get/:id',(req, res) => {
  let sql = "SELECT * FROM product WHERE product_id="+req.params.id;
  let query = conn.query(sql, (err, results) => {
    if(err) throw err;
    res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
  });
});
 
//add new product
route.post('/add',(req, res) => {
  let data = {product_name: req.body.product_name, product_price: req.body.product_price};
  let sql = "INSERT INTO product SET ?";
  let query = conn.query(sql, data,(err, results) => {
    if(err) throw err;
    res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
  });
});

module.exports = route;

You can now access these locations at the paths /products/get, /products/get/:id, /products/add and etc.

P.S.: You add a little mistake on the 'use strict' line it should be 'use strict' not 'user strict'

Sign up to request clarification or add additional context in comments.

1 Comment

It's working, I greatly appreciate your help. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.