# NodeJs Routing
How to set up basic Express web server under the node js and Exploring the Express router.
# 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).
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" } }
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"); });
For more detail on setting node server you can see [here][1].
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/
You Will see something like (opens new window)
Accessing Parameter in Routing
You can access the parameter from url also, Like http://example.com/api/:name/ (opens new window). So name parameter can be access. Add the following code into your server.js
router.get("/user/:id",function(req,res){
res.json({"message" : "Hello "+req.params.id});
});
Now restart server and go to [http://localhost:3000/api/user/Adem][4] (opens new window), the output will be like (opens new window).
# Remarks
At last, Using Express Router you can use routing facility in you application and it is easy to implement.