简体   繁体   中英

Nginx/Nodejs/Handlebars static files 404

I have a nodejs app with handlebars, and an nginx reverse proxy sitting on an ubuntu server. No matter what combination of app.use(express.static('public')) and linking stylesheets I use, I only get a 404 on the stylesheet. Here's my code:

const express = require('express')
const path = require('path');
const hbs = require('hbs');

const app = express();
app.set('view engine', 'hbs');
app.use(express.urlencoded({ extended: true }))
app.use(express.static('public'));
hbs.registerPartials(path.join(__dirname, 'views/partials'));


app.get('/', async (req, res) => {

  if (req.query.prompt) {
    prompt = req.query.prompt;
  }

  res.render('index', {
    prompt: prompt,
  });

})

app.listen(8080, () => console.log('Listening on port 8080.'))

And this:

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="index.css">
</head>

<body>
    <img src="profile.jpg" alt="photo">
</body>

</html>

Any help would be much appreciated, And yes: there are a lot of potential duplicates to this question but nothing has helped me :/

I've tried all combinations of /public, /index.css, /public/index.css etc.

My file structure looks like this: 在此处输入图像描述

I found the fix, and it wasn't nodejs or handlebars--it was removing the following line in my server block:

try_files $uri $uri/ =404;

I'll leave this here for anyone who might find this useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM