Skip to content

Instantly share code, notes, and snippets.

@Shalomeev
Last active January 12, 2018 09:57
Show Gist options
  • Save Shalomeev/a04c7e4cb46e3728844e3a83dca7d576 to your computer and use it in GitHub Desktop.
Save Shalomeev/a04c7e4cb46e3728844e3a83dca7d576 to your computer and use it in GitHub Desktop.
Let to add express-js routes from modules.

routes/student.js

const express = require('express');
const route = express.Router();

route.get('/', (req, res) => {
  res.send('Hello from student!');
});

route.get('/create', (req, res) => {
  res.send('Hello from create route!');
});

route.get('/list', (req, res) => {
  res.send('Hello from list route!');
});

module.exports =  {
    route
};

routes/index.js

const express = require('express');
const studentRoute = require('./student').route;

const routes = express.Router();

routes.use('/student', studentRoute);

module.exports = {
    routes
};

server.js

const express = require('express');
const {routes} = require('./routes/index');

const app = express();
const port = process.env.PORT || 3000;

app.use('/', routes);

app.listen(port, () => {
    console.log(`Server started on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment