Reputation: 675
This is my server.js
file:
var express = require('express'),
app = express();
app
.use(express.static('./public'))
.get('*',function (req,res) {
res.sendfile('/public/main.html');
})
.listen(3000);
This is my main.html
:
<!DOCTYPE html>
<html>
<head>
<titel>Contacts</titel>
<base href'/'>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Contatcs</h1>
</div>
</div>
</body>
</html>
And the folder structure:
Upvotes: 37
Views: 184546
Reputation: 1
l was also face same kind of problem. And did all the above solution but nothing has happened. Then I deleted my public, index.html
file and recreate it and use this code:
return res.sendFile( __dirname + '/public/index.html');
Upvotes: 0
Reputation: 5066
For my case I used nestjs with ServeStaticModule and it helps to add serveRoot option:
ServeStaticModule.forRoot({
// play with options below
rootPath: path.resolve(`./src/assets`), // I prefer to use resolve in your case it can be better to use path.join
serveRoot: '/assets',
})
So then I found files on: http://localhost:3000/assets/1.png
Other options : ServeStaticModuleOptions
Upvotes: 1
Reputation: 111
__dirname has been deprecated and the suggested solutions may not work currently (August 2021). In Express 4.x this has been simplified and you wouldn't need to use 'path' module, you just have to specify a root option like this:
app.get('*', (req, res) => {
res.sendFile('main.html', {root: 'public'});
});
Upvotes: 8
Reputation: 1291
I had the same problem. It appears that my client code was somehow not "in sync" with my server code (I'm not too familiar with the inner workings of node/express/angular and how they affect each other so "in sync" is the best I can do). I had only rebuilt my server code (after having added one nodejs library and updated another, among other server code changes) with this:
npm run build.server
(for my project this only builds server side. I run this sometimes when I'm testing server changes because rebuilding the client code as well, without a watch, can take a long time)
Once I rebuilt the client code this issue was resolved:
npm run build -- dev
(for my project this builds everything, server and client code)
Upvotes: 1
Reputation: 11
server.js
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/dist/projectName'));
app.post('/*', function(req, res){
res.sendFile(__dirname + '/dist/projectName/index.html');
});
app.listen(4200);
Upvotes: 1
Reputation: 51
Just use this : res.sendFile(__dirname + '/main.html');
It'll definitely work. :)
Upvotes: 1
Reputation: 9568
res.sendfile('/public/main.html');
should be changed to
res.sendfile('./public/main.html');
Upvotes: 2
Reputation: 139
I had a similar issue when I referred to dist
folder.
the relative path to index.html was:
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/project-name/index.html'));
});
Upvotes: 3
Reputation: 19
my server.js
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/dist/projectName'));
app.get('/*', function(req, res){
res.sendFile(__dirname + '/dist/projectName/index.html');
});
app.listen(4200);
Upvotes: -1
Reputation: 2270
Since both the server and the index file are INSIDE the "public" directory, you can simply use :
res.sendfile('./main.html');
To answer the question in the comments : In Express 4.x, the sendfile
method was replaced by the sendFile
method (all lowercase -> camelCase). Probably just an oversight in early versions, that got fixed in the latter.
Upvotes: 17
Reputation: 91
For me using the "." in the path didn't work, instead, I tweaked it as:
res.sendFile(__dirname + '/public/main.html');
Upvotes: 5
Reputation: 660
Though OP's cause is different for this error I got the same error due to a different reason so I am posting this for other's who come here.
I had a server-side shell script that was changing the current directory. Since we use relative paths in sendfile
I started seeing this error after this script would run. This shell script was being run by Node.
Upvotes: 0
Reputation: 11
I had the same problem. After emailing heroku, mine was a case sensitivity problem. One of my files was in all caps, and i had to make adjustments from there.
Upvotes: 0
Reputation: 1644
You missed the dot. Keep in mind relative directory is
res.sendfile('./public/main.html');
Upvotes: 3