Skip to content

Instantly share code, notes, and snippets.

@minhajul-islam
Last active March 13, 2021 14:59
Show Gist options
  • Select an option

  • Save minhajul-islam/76efc39bba78bf586197af34c9a2289e to your computer and use it in GitHub Desktop.

Select an option

Save minhajul-islam/76efc39bba78bf586197af34c9a2289e to your computer and use it in GitHub Desktop.

Router

  • app.js
const express = require('express');
const  http = require('http');
const  fs = require('fs');
const app = express();
const adminRouter = express.Router();
const server = http.createServer(app);


server.listen(8081, err => {
  if (err) { process.exit(1); }
  console.log('Server is up and running on port number 8081.');

  fs.readdirSync(path.join(__dirname, '/config/routes')).map(file => {
    require('./config/routes/' + file)(app);
  });
});

module.exports = app;

  • adminRouter.js
const express = require('express')
const app = express()
const adminRouter = express.Router()

// Only log for admin router
const loggerForAdmin = (req, res, next) => {
  console.log('This logging modleware only for admin router')
  next()
};

adminRouter.all('*',loggerForAdmin)

adminRouter.get('/', function (req, res) {
  res.send('Dashboard')
});

adminRouter.get('/login', function (req, res) {
  res.send('')
});

module.exports = app => {
  app.use('/admin', adminRouter)
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment