Lets create a server.js file. and add the routing information as:
//routing
app.get('/', function(req, RES){
res.redirect('/views/index.html'); //index.html file must be created inside views folder
});
Working Example server.js:
var express = require('express');
var app = express();
//setting up the static files for hosting
app.use(express.static(__dirname + '/'));
//routing
app.get('/', function(req, RES){
res.redirect('/views/index.html');
});
//... other routings
app.get('/landing', function(req, RES){
res.send('In landing page');
});
//end of routing
//launching application on localhost:1432
app.listen(1432, function(req, RES){
console.log('server loaded on port 1432');
});
Here,
var express = require('express'); =>> it requires express folder inside the node_modules folder. For this execute the following command:
npm install --save-dev express
also, give any port for the server as:
//launching application on localhost:1432
//you can put whatever port you want but that shouldn't be already used.
app.listen(1432, function(req, RES){
console.log('server loaded on port 1432');
});
Now, go to the cmd and enter:
node server //or node server.js
then, it will display the following console.log msg:
server loaded on port 1432
After that, if you enter
http://localhost:1432/views/index.html
in the browser, it will work.
No comments:
Post a Comment