Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

separate file for routes in express

Tags:

I was wondering how do I move all of my api routes in express into a separate routes.js file from my server.js file

I have a long list of api routes using app.use() for each route. So each route is in its own file, e.g. movies.js, movie.js but when I list these it makes for a long list in server.js

So I want to remove the list of api endpoints section from the below server.js out to a routes.js file.

Here is what I have currently:

server.js

import path from 'path' import express from 'express' import webpack from 'webpack' import webpackDevMiddleware from 'webpack-dev-middleware' import webpackConfig from './webpack.config.dev'  const app = express();  /* api endpoints, can be many more, I want them in routes.js */     app.use('/api/movies', require('./src/api/routes/movies')) app.use('/api/movie', require('./src/api/routes/movie'))  app.use(webpackDevMiddleware(webpack(webpackConfig), {   publicPath: webpackConfig.output.publicPath }));  app.use('/public', express.static(__dirname + '/public'))      app.get('*', function(req, res) {   res.sendFile(path.join(__dirname, 'index.html')); });   app.listen(3000, 'localhost', function (err) {   if (err) {     console.log(err);     return;   } }) 

An example route

movies.js

var express = require('express'); var request = require("request"); var router = express.Router();  router.get('/', function(req, res) {   res.json({}) });  module.exports = router; 
like image 762
svnm Avatar asked Mar 02 '16 13:03

svnm


People also ask

How do routes work in Express?

Routing refers to how an application responds to a client request to a particular endpoint, which is a URI (or a path) and a specific HTTP request method (GET, POST, etc.). Each route can have one or more handler functions, which are executed when the route is matched. app is an instance of express.

What does .USE do in Express?

The app. use() method mounts or puts the specified middleware functions at the specified path. This middleware function will be executed only when the base of the requested path matches the defined path.

What is route file in node JS?

Routing defines the way in which the client requests are handled by the application endpoints. Implementation of routing in Node. js: There are two ways to implement routing in node.


1 Answers

You can try to use the following modular approach. Define controller files having logic per feature. e.g. movie.

movieController.js

module.exports = {     getMovie : function(req, res){        //do something     },     getMovies : function(req, res){        //do something     },     postMovie : function(req, res){        //do something     } } 

Then, reference that controller in routes files and simply plug those functions.

routes.js

var express = require('express'); var movieCtrl = require('./movieController');  var router = express.Router();  router.route('/movie').get(movieCtrl.getMovie); router.route('/movie').post(movieCtrl.postMovie); router.route('/movies').get(movieCtrl.getMovies);  module.exports = router; 

And, in app.js, mount the routes to suitable location, e.g. /api

app.js

var routes = require('./routes'); app.use('/api', routes); 
like image 64
Mukesh Sharma Avatar answered Nov 03 '22 21:11

Mukesh Sharma