How to start node js?

How to install node js?
Node js is an open-source server, It provides the runtime environment for the executing javascript code from the instead of the browser. We create a server for establishing API and server-side code.

First, we need to install node js, you can go and download from here:

Now you can go with your cmd or vs code. We need to create a folder where you want to start Node new project. Use this command "npm init" as below:
F:\node>npm init

Then we need to give package name only on the lowercase character or you can give any numbers or with number and character as nodefirst without space. When enter your package name and press enter button then you will found more steps as given below. All options not required you can fill as you wish.

Press ^C at any time to quit.
package name: (nodefirst) 12she
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)

when complete all step then you will found your node setup now done as given below screenshot.

After then create a file "server.js" and type any code in javascript. Then go to cmd and type command "npm start" and you will found your javascript result.


Note: When you will modify your javascript file then needs to restart your node server again.

If you don't want to restart again and again with command line then need to install nodemon. Go to your cmd and use this command:
 npm install -g nodemon 

Then need to add "start" : "nodemon ./server.js" on the package.json file in script section. And then start the server using this command npm start after then you can change and save it then not required start again and again. See as below screenshot.


Also, you can see your result at browser so need to install expressjs with this command: npm install express

After installing express then need to code something like below:

var express = require('express')
var app = express()

app.get('/', function(req,res){
    res.send('Sheo test')
})

app.listen(3000) // this is server port you can modify according to you as 3001

Now will go to the browser and type manually http://localhost:3000/ then you will find your result at the browser as below screenshot: but need to restart your server when you will update anything your page.

No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.