0

so I know how to get params from URL the normal way:

router.get('/products/:id', (request, response, next) => {
   let id = request.params.id;

But when I want to refactor the code to many files, the code becomes something like:

const productService = require('../services/productService');
router.get('/products/:id', productService.getProductList);

How can I get the "id" from URL now?

The productService.js file:

var mongodb = require('mongodb');
const db = require('../models/mongoUtil.js').getDb();
const getProductList = async (request, response) => {
    try {
        let id = request.params.id;
        let object_id = new mongodb.ObjectId(id);

        const result = await db.collection('product_list').findOne({ '_id': object_id });
        response.json(result);
        console.log(result);
    } catch (err) {
        console.log(err);
    }
};

module.exports = {
    getProductList
};

Thank you. I am new to Node Js.

HieuNguyen96
  • 3
  • 1
  • 3

0 Answers0