Basic node.js application

Our Goals –
•    Create a Simple Webpage with the help of NodeJS
To do so –
1.    Install Node JS from NodeJS website
2.    Create A folder “myschool”
3.    Crete a File “app.js” inside “myschool” folder
4.    Open comand prompt and navigate to the folder.
5.    We will create “package.json” via comand prompt.
6.    Type “npm init”
7.    Provide the required data for our sample app and at last step right “yes” to complete ,like –

 

8.    Now our “package.json” file successfully created at our folder.
9.    Now install “express” on our node project by typing “npm install express –save”
10.    And the out put will be like –

11.    And “node_modules” folder has created on our project folder –
null

12.    Now create a Node server to run our app.
13.    Open “app.js” file and paste the following code.

14.    Now run this app using “node app” command at command prompt.
15.    If you are runninf first time nodejs then probably windows firewall will ask for access and you have to give the access by clicking on “Allow access” button.

 

16. Now at command promt the output will be –

17. Open your browser and type http://localhost:5000/ and it will show the result as –

18. And if you check the console, the log will displayed as –

19. To terminate the server press “Ctrl+c”

Source Code :

var express = require('express');
var app = express();
app.get('/',function(req,res){
res.send('Hello World');
console.log('user accessing home page');
});
app.listen(5000,function(){
console.log('server running on port 5000');
})

Thank you.
Note : After each changes on our code we need to stop and start our server. To ignore this we can install “nodemon” globally so it will automatically reflect when we mage any changes on the code.
Install nodemon in command prompt as – “npm install nodemon –g”

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *