I want to deploy my MEAN stack application but when I am opening the URL after deploying the application, It is downloading the index.html file of the angular app instead of rendering.
I have my Node.js code in backend folder. I ran ng build command in the angular app and stored the build in backend/public folder.
Then made some changes in the App.js file so that on running node server.js, It should launch both the angular and Node js app.
My App.js file
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require("mongoose");
const path= require('path');
const userRoutes = require("./routes/user");
const jobRoutes = require("./routes/job")
const app = express();
mongoose.connect("Connection String",
{ useNewUrlParser: true }, { useUnifiedTopology: true }, {useFindAndModify: false} )
.then(()=>{
console.log("Connected to Database");
})
.catch(()=>{
console.log("Connection Failed");
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.use((req,res,next)=>{
res.setHeader("Access-Control-Allow-Origin","*");
res.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.setHeader("Access-Control-Allow-Methods","GET, POST, PATCH, DELETE, OPTIONS");
res.setHeader('Content-Type', 'application/x-www-form-urlencoded');
next();
});
app.set({
'Content-Type': 'text/html'
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.use("/api/user", userRoutes);
app.use("/api/job", jobRoutes);
module.exports = app;
My Folder Structure for Deployment
public folder contains content of ng build
I want to open the angular app instead of downloading index.html file when I open the hosted website.
question from:
https://stackoverflow.com/questions/65859407/facing-problem-while-deploying-mean-stack-application 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…