I am trying to run my localhost node.js server but it is not loading in any browser, i am not getting any error as well just a buffering symbol on the browser. My code is running fine on VS Code. Here is the code:
server.js code:
const http = require("http");
const app = require("./app");
const port = 3000;
const server = http.createServer();
server.listen(port);
app.js code:
const express = require("express");
const app = express();
app.use((req, res, next) => {
res.status(200).json({
message: "it works!",
});
});
module.exports = app;
This is not working for any node.js code
VS Code is running fine No error or nothing is being shown
Same thing is happening for my html pages, images attached below: HTML page when clicked on Go Live, but it works if i double click the file directly without vs code
Please help me out
I tried many things but it just won't work, I re-installed node.js, I did reset my networks as well I don't understand the problem.
CodePudding user response:
In your code you should pass your Express App to createServer
const http = require("http");
const app = require("./app");
const port = 3000;
const server = http.createServer(app);
server.listen(port);
CodePudding user response:
You have defined app in server.js and never used. Simply you can use the below in the server.js file
const express = require("express");
const app = express();
const port = 3000;
app.use(express.json());
app.get("/", (req, res) => {
res.json({ message: "Hello, World!" });
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: "Internal Server Error" });
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Ive also added err to capture error.