DigitalOcean nodejs cannot run express

August 4, 2019 97 views
Node.js DigitalOcean

I installed nodejs and express but it cannot run my Main.js correctly

it shows:
Cannot GET /Main/

/etc/nginx/sites-available/digitalocean:

 location /Main/ {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

Main.js:

var express = require('express');
var app = express();

app.listen('8080', function () {
    console.log('Test');
});
1 Answer

Hi there @edmundng20062,

In your Main.js script, you've started the express application but have not defined any routes, which means that when you request /Main/, you haven't got a page to serve and so get Cannot GET /Main/.

You can define a GET route like this, which will return "Hello World!" when you navigate to /Main/.

app.get('/Main/', function (req, res) {
  res.send('Hello World!');
});

Your full script should then look like this:

var express = require('express');
var app = express();

app.get('/Main/', function (req, res) {
  res.send('Hello World!');
});

app.listen('8080', function () {
    console.log('Test');
});

You can take a look at the express.js documentation for further information on how you can define routes for your express application.

https://expressjs.com/en/starter/hello-world.html
https://expressjs.com/en/guide/routing.html
https://expressjs.com/en/4x/api.html

Hope that helps!
- Matt.

Have another answer? Share your knowledge.