Search code examples
node.jsexpressubuntu-server

Ubuntu Node.js Express Server Error "Cannot GET //test"


I've tested the following node.js express server on my local maschine (Windows 10) and it worked as expected.

// Imports
const express = require('express');
const { check, validationResult } = require('express-validator/check');
const bodyParser = require('body-parser');
const path = require('path');
const session = require('express-session');

// app
const app = express();

// Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

// Get static path
app.use(express.static(path.join(__dirname, 'public')));

// Get requests
app.get('/test', (req, res) => {
    res.end('Hello World');
});

// Validation test
app.post('/testSubmit', [
    check('mail').isEmail(),
    check('password').isLength({ min: 5 })
], (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
    }

    console.log(req.body);

    res.end();
});

// Start listening
app.listen(3000, function() {
    console.log('Server started on Port 3000');
});

Error:

But now I uploaded my code to the ubuntu server and there I get the error "Cannot GET //test" when I try ro call myserv/api/test.

The same accures with myserv/api/testSubmit ("Cannot POST //testSubmit").

What works:

Static content works on ubuntu, if I call myserv/api/ I get the content of index.html which is located in the "public" folder.

What i have tested so far:

  • Removing the static content code > no effect
  • Updated Node.js and NPM > no effect
  • npm install > no effect
  • chmod -R 777 on my node folder > no effect

Does anybody know what I'm missing?


Solution

  • I have solved the problem, it has nothing to do with node. The problem was the Reverse Proxy which i use to run node.js together with apache.

    Windows:

    ProxyPass http://192.168.3.132:3000
    ProxyPassReverse http://192.168.3.132:3000
    

    Ubuntu:

    ProxyPass http://192.168.1.12:3000/
    ProxyPassReverse /api http://192.168.1.12:3000/
    

    I just had a / after the URL (only on my Ubuntu server) that doesn't belong there