MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS Routing

Express Web Server Routing

Creating Express Web Server


Express server came handy and it deeps through many user and community. It is getting popular.

Lets create a Express Server. For Package Management and Flexibility for Dependency We will use NPM(Node Package Manager).

1. Go to the Project directory and create package.json file. package.json

{
 "name": "expressRouter",
 "version": "0.0.1",
 "scripts": {
 "start": "node Server.js"
},
 "dependencies": {
 "express": "^4.12.3"
}
}

2. Save the file and install the express dependency using following command npm install. This will create node_modules in you project directory along with required dependency.

3. Let's create Express Web Server. Go to the Project directory and create server.js file. server.js

var express = require("express");
var app = express();
//Creating Router() object
var router = express.Router();
// Provide all routes here, this is for Home page.
router.get("/",function(req,res){
res.json({"message" : "Hello World"});
});
app.use("/api",router);
// Listen to this Port
app.listen(3000,function(){
console.log("Live at Port 3000");
});

4. Run the server by typing following command.

node server.js

If Server runs successfully, you will se something like this.

5. Now go to the browser or postman and made a request

That is all, the basic of Express routing.
Now let's handle the GET,POST etc.

Change yous server.js file like

var express = require("express");
var app = express();
//Creating Router() object
var router = express.Router();
// Router middleware, mentioned it before defining routes.
router.use(function(req,res,next) {
 console.log("/" + req.method);
 next();
});
// Provide all routes here, this is for Home page.
router.get("/",function(req,res){
 res.json({"message" : "Hello World"});
});
app.use("/api",router);
app.listen(3000,function(){
 console.log("Live at Port 3000");
});

Now if you restart the server and made the request to

http://localhost:3000/api/


Conclusion

In this page (written and validated by ) you learned about NodeJS Routing . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Creating a Nodejs Library that Supports Both Promises and ErrorFirst Callbacks.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.