First of all you should have node.js installed.
Here is how a simple HTTP server can be implemented with Express.
Initial setup
First Express should be installed globally and locally with NPM. If you have installed express ignore these two steps.
Here is the complete code for that server
Here is how a simple HTTP server can be implemented with Express.
Initial setup
First Express should be installed globally and locally with NPM. If you have installed express ignore these two steps.
- Install Express globally with NPM(Node Package Manager)
npm install -g express
- Install express locally
npm install express
npm install express --saveif you using a package.json file to track your project
- Require "Express" package in our Application
var express = require('express');
-
Create an Express object
var app = express();
-
Create routing
Here we pass expected URL pattern('/') and required functionality as a function to express method get()app.get('/',function(req,res){ res.send('Hello Express'); }); -
Creating a server with a listening port
var server = app.listen(3000,function(){ console.log('Listening on port 3000'); }); -
Run your server with
node fileName.jsand try it with a browser
Here is the complete code for that server
var express = require('express'); //importing the express library
var app = express(); //create an express object
//routing
app.get('/',function(req,res){
res.send('Hello Express');
});
//create a server
var server = app.listen(3000,function(){
console.log('Listening on port 3000');
});
No comments:
Post a Comment