I have the following code for an router file in express.
import express from 'express';
import _ from 'lodash';
import { Devices, OwlElecMonitors } from '../models/';
var router = express.Router();
router.get('/api/devices/:id',function (req, res) {
console.log(req);
Devices.getDevicesByUserId({ userId: req.params.id },function(err, resp) {
res.send(resp);
});
});
export default router;
and I am trying to import it into the main file using the following code
import api from './routes';
app.use('/api', api);
But the code returns a 404 error. Where am I going wrong ? What changes do I need to make for this to work ?
Node js doesn't support ES6 import directly. If we try to use import for importing modules directly in node js it will throw out the error. For example, if we try to import express module by writing import express from 'express' node js will throw an error as follows: Node has experimental support for ES modules.
Your api is currently set to /api/api/devices/:id
.
Remove the /api
from the router get definition:
router
.get('/devices/:id',function (req, res) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With