Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

node.js - Facing problem while deploying MEAN stack application

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...