Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Express Js Object Last Main Initilized with ()
- const app = require('express')();
- // Body Parser Only Library Imported
- const bodyParser = require('body-parser');
- // Telling App To Use Body Parser
- app.use(bodyParser.json());
- // Note - Every route get or post needs (req,res)
- // So it is must to use
- // Base Url Method Most Basic Method of routing
- app.get("/",(req,res)=>{
- res.send("<h1>Hello World!</h1>");
- });
- // URL with URL Embeded Paramerters Method
- // Here name is subblied in URL
- // And Can be accessed using req.prams.name
- app.get("/greet/:name",(req,res)=>{
- const name = req.params.name;
- res.send("Hello : "+name);
- });
- // Querry Parameters Are Supplied after a ?
- // They state not essential data from client
- // It can be accessed using req.query.variableName
- app.get("/greet2/:name",(req,res)=>{
- const name = req.params.name;
- const surname = req.query.sname;
- res.send("Hello 2 : "+name+" "+surname);
- });
- // Just like get post method is there
- // Post method is secured and is utilized for
- // Login and other confedinital work
- // You can access post data using body-parser
- // app.body.variableName
- app.post("/login",(req,res)=>{
- console.log(req.body);
- const user = req.body.userId;
- const password = req.body.password;
- if(password == "1234"){
- res.send("Logged In");
- }
- else{
- res.send("Invalid Username And Password");
- }
- });
- // By default if path is called get route always respond first
- app.get("/login",(req,res)=>{
- console.log("Login Get Called");
- res.send("Wrong Address");
- });
- // Here we are telling our app object defined in
- // Line number 1 to listen or work in which port
- // This can be any number in this case 3000
- app.listen(3000,()=>{
- console.log("Server Is Running");
- }
- );
Add Comment
Please, Sign In to add comment