Hi guys, I’m kinda new to all the stuff related to deployment, servers, etc.
I followed tutorials explaining how to setup a Linux 18 server with nginx, and the example worked properly.

However, when I redirect the proxy to my application and then try to access it via a browser, I have a 502 error.

Here’s the code of my node app :

const https = require("https");
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const cors = require("cors");
const mustacheExpress = require("mustache-express");
dotenv.config();

// Connect to DB
mongoose.connect(process.env.DB_CONNECT, { useNewUrlParser: true }, () => {
  console.log("connected to DB !");
});

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

// Middleware
app.use(express.json());

// Templating
app.engine("html", mustacheExpress());

//import routes
const authRoute = require("./routes/auth");
const devisRoute = require("./routes/devis");
const messageRoute = require("./routes/message");
const actuRoute = require("./routes/actu");

//Routes middlewares
app.use("/api/user", authRoute);

app.use("/api/sendDevis", devisRoute);

app.use("/api/message", messageRoute);

app.use("/api/actu", actuRoute);

// app.get("/", (req, res) => {
//   return res.status(200).json({ status: "OK" });
// });

app.use(express.static("../front-end/dist/natan-website"));

app.get("/", function (req, res, next) {
  res.redirect("/");
});


app.listen(3000, () => console.log("Server is running !"));

Does anyone have an idea of the mistake I’ve made ?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

So after some research, my code was not correct, middlewares were missing. Here’s the good one :

var express = require(‘express’);
var app = express(); // create our app w/ express
var morgan = require('morgan’); // log requests to the console (express4)
var bodyParser = require('body-parser’); // pull information from HTML POST (express4)
var methodOverride = require('method-override’); // simulate DELETE and PUT (express4)
const mongoose = require(“mongoose”);
const dotenv = require(“dotenv”);
const mustacheExpress = require(“mustache-express”);

dotenv.config();

// configuration =================

mongoose.connect(process.env.DB_CONNECT, { useNewUrlParser: true }, () => {
console.log(“connected to DB !”);
});

app.use(express.static(“../front-end/dist/website”));

app.use(morgan('dev’)); // log every request to the console
app.use(bodyParser.urlencoded({'extended’:'true’})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json’ })); // parse application/vnd.api+json as json
app.use(methodOverride());

// Middleware
app.use(express.json());

// Templating
app.engine(“html”, mustacheExpress());

//import routes
const authRoute = require(“./routes/auth”);
const devisRoute = require(“./routes/devis”);
const messageRoute = require(“./routes/message”);
const actuRoute = require(“./routes/actu”);

//Routes middlewares
app.use(“/api/user”, authRoute);

app.use(“/api/sendDevis”, devisRoute);

app.use(“/api/message”, messageRoute);

app.use(“/api/actu”, actuRoute);

// listen (start app with node server.js) ======================================
app.listen(3000);

Submit an Answer