Why my static HTML file not serving in Express.js - node.js

I don't know why my static html files are not serving in express.js when I click on about it not serving about.html but instead only plain text about which i written in res.end('about')
don't know what is the issue.
app.js
const express = require('express');
const app = express();
const port = process.env.PORT || 80;
const path = require('path');
const static_path = path.join(__dirname,"../public");
app.use(express.static (static_path));
console.log(static_path)
app.get('/',(req,res)=>{
res.send('index')
})
app.get("/about", (req,res)=>{
res.send("about")
});
app.get('*',(req,res)=>{
res.status(404).send("404 page is not found")
})
app.listen(port, ()=>{
console.log(`this website is running on ${port}`)
})

use res.sendFile() instead of res.send()
https://expressjs.com/en/api.html#res

Related

React router not working when navigating using url

I am getting an error Cannot GET /'page' when trying to navigate using url to a path in React Router. Navigating from application works perfectly fine. Here is my server.js:
const express = require('express');
const app = express();
const port = process.env.PORT || 3001;
const path = require('path');
app.use(express.static('build'));
app.listen(port, () => {
console.log('Server is up on port: ' + port);
})
I've tried adding this code snippet to my server but it didn't work:
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, './public/index.html'), function(err) {
if (err) {
res.status(500).send(err)
}
})
})

find the error while deploying the node.js app to server

I found the error while deploying the node app on the server. the index.js file code
const connectToDb = require("./DBconfig");
const express = require('express')
const router = express.Router();
var cors = require('cors');
connectToDb();
const app = express();
const port = process.env.PORT | 5000;
app.use(cors());
app.use(express.json());
connectToDb();
app.use( '/api/user' ,require('./routes/user') );
app.use( '/api/post' ,require('./routes/post') );
app.get('/', (req, res) => {
res.send('The site is start now')
})
if(process.env.NODE_ENV === "production"){
app.use(express.static("client/build"));
const path = require("path");
app.get("*",(req,res)=>{
res.sendFile(path.resolve(__dirname,'client','build','index.html'))
})
}
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
when I deploy the app and install the node in the server like how I install the node in server I attached the picture in which I show the installation of the node. and above I add the index.js code. It works properly in the local machine but if I hit the URL of backend backendURL it show the service unavailable error message kindly give me the solution to solve this problem.

No response from nodejs api

This is my code i tried to send a request through http://localhost:3000/api/post/article using postman but i received cannot get as error.
It's working without using router.get but instead using app.get, so i think the problem is with the router.
This is the server.js file
const http = require("http");
const app = require("./app");
app.set("port", process.env.PORT || 3000);
const server = http.createServer(app);
server.listen(process.env.PORT || 3000);
this is the app file
const express = require("express");
const postRoutes = require("./routes/post");
const app = express();
app.use("api/post", postRoutes);
module.exports = app;
This is the router file
const express = require("express");
const router = express.Router();
const postCtrl = require("../controllers/post");
router.get("/article", postCtrl.specArticle);
module.exports = router;
This is the controller file
module.exports.specArticle = (req, res) => {
res.status(200).json({ message: "working currently" });
};
Change this:
app.use("api/post", postRoutes);
to this:
app.use("/api/post", postRoutes);
As best I know, all paths in your route handlers should start with / (at least I have no idea why you would ever not start them with a /).

syntax error: unexpected token < after replace controller in AngularJS

I use the sample of Auth0 examples AngularJS. I need to rewrite this sample and replace the controllers js file to a folder - controllers. But after doing that I get an error: syntax error: unexpected token <
I figure out that it is may server problem
server configuration on node. js is :
const express = require('express');
const app = express();
const path = require('path');
app.use('/', express.static(__dirname + '/'));
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
const hostname = '0.0.0.0';
const port = 3000;
const server = app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
What problem of this server? Thank you.

Use of express.static middleware

Both below codes serve index.html at localhost:3000/ when the server is started.
Use of express.static
const path = require('path');
const express = require('express');
const PORT = process.env.port || 3000;
const publicPath = path.join(__dirname, '../public');
var app = express();
app.use(express.static(publicPath));
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
})
Use of app.get
const path = require('path');
const express = require('express');
const PORT = process.env.port || 3000;
const publicPath = path.join(__dirname, '../public');
var app = express();
app.get('/',(req,res) => {
res.sendFile(publicPath + '/index.html');
})
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
})
So why someone would choose express.static over app.get to serve static html file. What's the use of static middle ware on express
The code that doesn't use express.static will fail when serving any other static page that isn't index.html, even if index.html included other static files(as a css) it would fail.

Resources