const express = require('express');
const app = express();
app.use(express.static('public'));
public means
//use method will be use whenever wanna make middlewares im our applications, middlewares will be used before responding requiest (mediator between request and before respond)
//routing ist auth - then controler - in auth is mediatior
// we create a folder called public, inside this we will place a style sheet, javascrpt, fonts, images we will place at public
one seperate directory for images,javascript files, css file that called as scaffolding
npm install express-generator -g
to make public folders
get api with star
app.get("/*list",(req,res,next) =>
{
let person="department listing"
res.send(person);
}
);
personlist or any thing before list
api process_get
json stringfy - combine response
app.get("/process_get",(req,res,next) =>
{
response=
{
first_name :req.query.first_name,
last_name :req.query.last_name
};
res.end(JSON.stringify(response));
}
);
post api
let bodyParser = require('body-parser');
var urlencodedParser= bodyParser.urlencoded({ extended: true });
post api needs url encoded and body-parser
urlencodedParesr - is like middleware
before api
app.post("/process_post",urlencodedParser,
function (req,res)
{
response=
{
first_name :req.body.first_name,
last_name :req.body.last_name
};
res.end(JSON.stringify(response));
}
);
post is from aoi or html form
api get form
app.get("/form",(req,res,next) =>
{
res.sendFile(__dirname + "/" + "one.html");
}
);
one.html
<form method="POST" action="http://localhost:8081/process_post">
<input type ="text" name="first_name">
<input type ="text" name="last_name">
<input type="submit" name="submit">
</form>
after submit post in above html form
it will reach process_post
takes taht data from post data and combine as stringify
connect to server
using below
var server = app.listen(8081,
function () {
var host= server.address().address;
var port= server.address().port;
console.log("listening at http://%s:%s", host,port);
});
No comments:
Post a Comment