Question

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


Submit an answer


This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.