MEAN.JS - Building Static Route Node Express



This chapter demonstrates building route for an application with Node and Express.

In the previous chapter, we created a node-express application. Navigate to project directory called mean-demo. Go to the directory by using below command −

$ cd mean-demo

Setting Up Routes

Routes are used as mapping service by using URL of an incoming request. Open the server.js file and setup the routing as shown below −

// modules =================================================
const express = require('express');
const app = express();

// set our port
const port = 3000;
app.get('/', (req, res) ⇒ res.send('Welcome to Tutorialspoint!'));

//defining route
app.get('/tproute', function (req, res) {
   res.send('This is routing for the application developed using Node and Express...');
});

// startup our app at http://localhost:3000
app.listen(port, () ⇒ console.log(`Example app listening on port ${port}!`));

Running Application

Next, run the application with the below command −

$ npm start

You will get a confirmation as shown in the image below −

Running Application

Now, go to browser and type http://localhost:3000/myroute. You will get the page as shown in the image below −

Node Express
Advertisements