Node Js basic tutorial - OneNightStudy.Com

Wednesday, 16 August 2017

Node Js basic tutorial


1.Go to the project folder then run command "npm init"  (will create package.json with the dependencies you need also will ask the details just skip in case not want to enter incase not installed the node first install node and npm)

2. now install the express(node framework) and jade (template engine)
npm install --save express jade

so this command will install this packages in the node modules folder

3. Now can install git and run the command in the project folder like 
git init (this will add the project in the git)
git status (to check file status in the git)

4. For ignoring the file and folders just create the file called .gitignore in the root folder  and write the name of folder/files to ignore eg.

node_modules (so this folder will be ignored by git)

so again check the status and add and then commit to git like:

git add -A

5. To install client side packages or libraries we can user bower so install bower like:
sudo npm install bower
bower init (this will create bower.json skip the input asked while running command if not know)

6. Now create the file called the .bowerrc for adding the path where the libraries will be stored here eg write in the file:
"directory": "public/vendor"   (also create the folders public and vendor)

7. Install the libraries using bower command:
bower install jquery --save
bower install toastr --save
bower install angular angular-resource angular-route --save (angular modules that are required)

8. Now in the node js main file from excecution starts is server.js (so create the file in the root folder) and write this code:
var express = require('express');
var env = "development";
var app = express();

app.set('views',__dirname+'/server/views'); //view path
app.set('view engine','jade'); //balde engine

res.render('index');   //route * and will redirect to index.jade

var port = 3030;
console.log('listen on'+port);

9. Now also create the server/views/index.jade and write the code:

h1 hello world

10. Now time to run on browser for that use:
node server.js

11. But while using node server.js every time we change the files we need to restart the server so to solve we will install the nodemong:

npm install nodemong -g  //-g for global installation

12.Now finally run the server with 
nodemon server.js   //server started at 3030 port

13. Go to browser and type the url http://localhost:3030/ and will be showing "Hello World";

This is the basic tutorial for angular and node js hello world program run.

1 comment: