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');
});
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!
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.
Hi!
I have a similar problem.
My Nginx Block:
server {
server_name MY_SITE www.MY_SITE;
root /var/www/MY_SITE/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_pass http://localhost:3000;
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;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/MY_SITE/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/MY_SITE/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = MY_SITE) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = MY_SITE) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name MY_SITE www.MY_SITE;
return 404; # managed by Certbot
}
When I use only node (code below) and access MY_SITE/api works perfectly.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.JS!');
}).listen(3000);
console.log('Server running at http://localhost:3000/');
When I use Express (below). MY_SITE/api return:
Route 1 => Cannot GET / api / Route 2 works!
How do I get Route 1 to work by changing the Nginx setting?
var express = require('express');
var app = express();
//return Cannot GET /api/
app.get('/', function (req, res) {
res.send('Hello World!');
});
//works!
app.get('/api', function (req, res) {
res.send('Hello World!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.