Home > database >  When i use express and use ''use'' request to get response this came in my chorm
When i use express and use ''use'' request to get response this came in my chorm

Time:02-24

iam new in node.js .iam faceing this problem .When i use express and use ''use'' request to get response to the chorome but this erro came in my chorme bowser Cannot GET. or my app.js file or index.js is in one same folder . folder name is static. Iam not good in english please help me. Or if i use nodemon togther than when i get to localhost the the loaclhost cannot and show this 'This site can’t be reached' i dont know how to fix it can any help me to fix this problem this the code

const express = require("express");
const app = express();
 const port = 8080;

app.use('/static',express.static('express/static'));

app.get("/", (req, res)=>{
    res.status(200).send("This is the first page of node express");
});

app.get("/about", (req, res)=>{
    res.send("This is the about page of node express");

});

app.post("/postabout", (req, res)=>{
    res.send("This is the postabout page of node express");

});

app.post("/erro", (req, res)=>{
    res.status(404).send("This page has been errored in code of 404");

});

app.listen(80,'127.0.0.1', () =>{
    console.log(`The application started and the port is ${port}`);
});
This the image of my code and chrome

CodePudding user response:

In app.listen you passed wrong port means you intialized port as 8080 but passed 80

app.listen(8080,() =>{
    console.log(`The application started and the port is ${port}`);
});

CodePudding user response:

Basically, app.use function uses a middleware when a particular API is called it does not a get request.

So try app.get instead of app.use it will work.

app.get('/static', (req, res) => {
  // Do your stuffs.
  return res.send(response)
})
  • Related