Unable to connect to the node app

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");

// 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", "*");
    "Origin, X-Requested-With, Content-Type, Accept"

// Middleware

// 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.get("/", function (req, res, next) {

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

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


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Accepted 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”);


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

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

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);