Bad Gateway Using Express

workspace
nodejs

#1

Trying to access my site:

From my terminal:

{ Error: ENOENT: no such file or directory, stat '/home/ubuntu/workspace/public/home/ubuntu/workspacesticky/html/index.html’
errno: -2,
code: ‘ENOENT’,
syscall: ‘stat’,
path: ‘/home/ubuntu/workspace/public/home/ubuntu/workspacesticky/html/index.html’,
status: 404 }

Whats going on?

I am using express js, and it started doing that at 10P Sunday. Are the servers going through maintenance?


#2

Can you post some of your code here, so we can make sure the problem isn’t there? Specifically, wherever you’re trying to get the index.html file (this may be a place where you run the .render() method). In this case, it’s likely that you’re attempting to load a file using an absolute path but express assumes it’s relative. It would also be helpful if you could screenshot the file tree so we can see your file structure.


#3

Here is my server.js, executed with “node server.js” at the terminal.

const express = require('express')
const app = express()

app.get('/', function (req, res, next) {
  //res.send('landing page');
  var options = {
root: __dirname + '/public/',
dotfiles: 'deny',
headers: {
    'x-timestamp': Date.now(),
    'x-sent': true
}
  };
  
  res.sendfile(__dirname + 'sticky/html/index.html', options, function (err) {
console.log(err);
  });
})

app.get('/save/:id', function(req, res){
  res.send("Asked for: " + req.params.id);
});

app.listen(8080, function () {
  console.log('Example app listening on port 8080 or 80!')
})

#4

The path might be off? The path being called out by the error message seems off (but hey, who am I to judge your naming conventions? :stuck_out_tongue: )

path: ‘/home/ubuntu/workspace/public/home/ubuntu/workspacesticky/html/index.html’,


#5

Should be as simple as changing this:

res.sendfile(__dirname + 'sticky/html/index.html', ...

To this:

res.sendFile(path.resolve(__dirname, 'sticky/html/index.html'), ...

path.resolve or path.join should work, and will resolve to the file. You can also use it like this, though it shouldn’t make a difference:

res.sendFile(path.resolve(__dirname, 'sticky', 'html', 'index.html'), ...

#6

I just changed to static paths, but will certainly update my code. Also, will I get the error every time there is a 404? I guess it doesn’t make a difference.

Thank!