Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure express router along with ES6?

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 ?

like image 357
Bazinga777 Avatar asked Nov 12 '15 18:11

Bazinga777


People also ask

Does Nodejs support ES6?

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.


1 Answers

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) { 
like image 166
Yuri Zarubin Avatar answered Sep 19 '22 19:09

Yuri Zarubin