Learning MEAN Stack: Express JS - 101
MEAN
Express JS is a web framework which makes it easier to build a web server in Node JS.
Express helps in writing the NodeJS code cleanly.
Installation
- npm install express --save
- Building and running HTML on a web server
No need to mention header with Content-Type etc. like done before in nodejs.
- var express = require('express');
- var app = express();
- app.get('/', function(req,res){
- res.send('<html><head></head><body><h1>Hello Sarthak, Welcome to express!</h1></body></html>')
- });
- app.listen(3000);
run as >nodemon file.js
2. Using it to build a API.
- var express = require('express');
- var app = express();
- app.get('/api', function(req,res){
- res.json({firstname: 'Sarthak', lastname: 'Srivastava'})
- });
- app.listen(3000);
Send request to localhost:3000/api
- var express = require('express');
- var app = express();
- app.get('/person/:id', function(req, res){
- res.send('<html><head></head><body><h1>Hello, Your ID is : '+req.params.id+'</h1></body></html>')
- });
- app.listen(3000);
request to http://localhost:3000/person/123
4. Using static files and middleware using Express
Middleware means code that sits between req. and res.
Where static files are HTML, CSS, IMG etc files
//middleaware
- app.use('/assets', express.static(__dirname, +'/public'));
- var express = require('express');
- var app = express();
- app.use('/assets', express.static(__dirname, +'/public'));
- app.get('/css', function(req,res){
- res.send('<html><head><link href = assets/style.css type = text/css rel=stylesheet></link></head><body><h1>Hello Sarthak, Welcome to express!</h1></body></html>')
- });
- app.listen(3000);
public/style.css
- body{
- background: pink;
- }
req to http://localhost:3000/css
4. Template and Template Engines in Express JS
Lets use EJS, it is similar to ASP.net, PHP etc.
Installing EJS
- npm install ejs --save
index.ejs
- <html>
- <head>
- </head>
- <body>
- <h1>
- Hello to EJS.
- </h1>
- </body>
- </html>
Person.ejs
- <html>
- <head>
- </head>
- <body>
- <h1>
- Person: <%= ID %> <br/>
- Person ID length: <%= ID.length %>
- </h1>
- </body>
- </html>
app2.js
- var express = require('express');
- var app = express();
- app.set('view engine','ejs');
- app.get('/', function(req, res){
- res.render('index');
- });
- app.get('/person/:id', function(req, res){
- res.render('Person', {ID: req.params.id});
- });
- app.listen(3000);
http://localhost:3000/
http://localhost:3000/person/sarthak
5. Rendering Query String
In a GET request the query string can be rendered from the API url.
GET/api?id=4&page=3
- var express = require('express');
- var app = express();
- app.set('view engine','ejs');
- //GET
- app.get('/person/:id', function(req, res){
- res.render('personGet',{
- ID: req.params.id,
- Qstr: req.query.Qstr
- });
- })
- app.listen(3000);
personGet.ejs
- <html>
- <head>
- </head>
- <body>
- <h1>
- Person: <%= ID %>
- <br/>
- Query String value: <%= Qstr %>
- </h1>
- </body>
- </html>
http://localhost:3000/person/tony?Qstr=123
6. Accessing POST request parameters
It can be done via body-parser module
- npm install body-parser --save
Since in POST request the query string is in the body of the request unlike the GET request.
Here for HTML data we will use URLencodedParser
js file
- var express = require('express');
- var bodyparser = require('body-parser')
- var app = express();
- app.set('view engine','ejs');
- var urlencodedparser = bodyparser.urlencoded({extended:false})
- app.get('/', function(req, res){
- res.render('bodyparserpost');
- });
- app.post('/person',urlencodedparser, function(req, res){
- res.send('Thank you for POST req.')
- console.log(req.body.firstname);
- console.log(req.body.lastname);
- })
- app.listen(3000);
ejs code file
- <html>
- <head>
- </head>
- <body>
- Fill the form:
- <form method="POST" action="/person">
- firstname: <input type="text" id="firstname" name="firstname" />
- <br/><br/>
- lastname: <input type="text" id="lastname" name="lastname" />
- <br/><br/>
- <input type="submit" value="submit"/>
- </form>
- </body>
- </html>
http://localhost:3000/
Comments
Post a Comment