Code Monkey home page Code Monkey logo

organizing-routes-in-express-js-and-node.js's Introduction

Organizing-routes-in-express-js-and-node.js

Instead of making app.js heaving with every single routing rule, try to organise it as suggested below. The your-app/app.js contains the below line of codes for the purpose of routing.

	var routes = require('./routes/index');
	var users = require('./routes/users');

	app.use('/', routes);
	app.use('/users', users);

So, I thought to make this things a bit easier and organize app.js file The structure that I kept in mind is like as below:-

	router/
	    routes/
	        users.js
	    index.js

In app.js, first of all comment

	var routes = require('./routes/index');
	var users = require('./routes/users');
	app.use('/', routes);
	app.use('/users', users);

The above 4 lines.

Add the below line of code just after var app = express(); :-

	var router = require('./routes')(app);

Then, here is the code of index.js route file:-

	var express = require('express');
	var router = express.Router();

	module.exports = function(app) {
	    app.use('/users', require('../routes/users'));
	};

The code of users.js route file is as follows:-

	var express = require('express');
	var router = express.Router();
	
	router.post('/', function (req, res) {
	    // handle a post request to this route
	    console.log("respond with a resource");
	});
	
	router.get('/info', function (req, res) {
	    // handle a get request to this route
	});
	module.exports = router;

To run:-

	http://localhost:3000/users

For more information you may visit:-

http://cj7.info/blog/organizing-routes-in-express-js-and-nodejs

Installation:-

git clone https://github.com/dinesh-rawat/Organizing-routes-in-express-js-and-node.js.git sudo node ./bin/www

organizing-routes-in-express-js-and-node.js's People

Contributors

dinesh-rawat-dev avatar dineshmds avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.