I created an ExpressJS app with handlebars templating engine and starting the app via npm start
looks all good to me since all assets are being loaded.
Here's my folder structure:
- public
- css
- svg
- uploads
- views
- layouts
> home.handlebars
And here's the relevant stuff of my index.js:
const express = require('express');
const exphbs = require('express-handlebars');
const path = require('path');
const fs = require('fs');
const port = 5000;
const index = express();
app.set('views', path.join(__dirname, '/', 'views'));
app.use(express.static(__dirname + '/public'));
app.engine('handlebars', exphbs({
defaultLayout: 'main',
layoutsDir: path.join(__dirname, '/', 'views', 'layouts')}
));
app.set('view engine', 'handlebars');
Here is the content of my now.json:
{
"version": 2,
"builds": [
{
"src": "index.js",
"use": "@now/node-server"
}
],
"routes": [
{
"src": "/",
"dest": "/",
"methods": ["GET"]
},
{
"src": "/justaroute",
"dest": "/",
"methods": ["POST"]
}
]
}
The Problem
As I said above using npm start
shows my working app but using now dev
my app can't find any assets (display a 404 in the console):
http://localhost:3000/css/icons.css net::ERR_ABORTED 404 (Not Found)
http://localhost:3000/uploads/[...].jpg 404 (Not Found)
.
It seems that
app.set('views', path.join(__dirname, '/', 'views'));
is setting the views path as root for the assets the views are loading.
Inside the views I load assets like this:
<link rel="stylesheet" type="text/css" href="css/icons.css">
I already tried to add /public/[...]
to the assets hrefs and getting the public folder explicitly by adding app.set('public', path.join(__dirname, '/', 'public'));
which resulted in Chrome still not finding the assets: http://localhost:3000/public/css/icons.css net::ERR_ABORTED 404 (Not Found)