Node package manager
package express
- instalation > $ npm install express
- uninstallation > $npm uninstall express
File package.json
It manages all the package in application.
1. Create node app creating packet.json file
> $ npm init
- enter all information..
File will be created.
Installing express
Express package :
- provides easy and flexible routing system
- Integrate with templates
- Middleware framework
> npm install express
simply install it and add list in json file on require list
> npm install express --save
simply install it and add list in json file On depencency list
- depencency means this package is required to run the app
- uninstalling with not remove from depencency list
> npm install
- this will install all package along with in dependency list
-----------------------------
Installing EJS
> npm nstall ejs -save
app.set('view engine','ejs');
------------------
Installing nodemon
Nodemon is package that is use to monitor app. So that changes in file can directly load in browser on browser refresh without reloading and restarting node server.
> npm install nodemon -g
- install globally dont have to install on every project individually
Express
Getting param
req.param.idreq.query.idreq.body.id
//to get GET /something?color1=red&color2=blue
app.get('/something', (req, res) => {
req.query.color1 === 'red' // true
req.query.color2 === 'blue' // true
})
// post param
app.post('/api/users/add', (req, res) => {
const userName = req.body.userName
const jobName = req.body.jobName
}
Templating with EJS
res.send("hello");res.sendFile( __dirname + "/file.html");
app.set('view engine','ejs');
//pass param to view file and render
...
res.render("profile", {person: req.params.name});
..
-------------------------
var express = require('express');
var app = express();
app.listen(3000);
app.set('view engine','ejs');
app.get("{route}", function );
app.post("{route}", function );
app.get("/", function( req, res) {
res.send(" this is home page");
} );
// route with param
app.get( "/profile/:id/:name" , function( req, res) {
var paramId = req.params.id;
res.send(" profile page with id " + parmId + " name is" + req.params.name);
res.render("profile", {person: req.params.name, id: req.params.id});
} );
profile.html
<div>
<%= data.name %>
</div>
---------------------------