I'm making an app in Angular 2/node.js and in order to render the homepage, I use the following code in routes.js:
app.get('*', function(req, res) {
res.sendFile('MEAN_project/app/index.html'); // load our public/index.html file
});
For some reason, when I start the server, I get the following error:
TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (c:\MEAN_project\node_modules\express\lib\response.js:404:11)
at c:\MEAN_project\app\routes.js:25:17
at Layer.handle [as handle_request] (c:\MEAN_project\node_modules\express\lib\router\layer.js:95:5)
at next (c:\MEAN_project\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (c:\MEAN_project\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (c:\MEAN_project\node_modules\express\lib\router\layer.js:95:5)
at c:\MEAN_project\node_modules\express\lib\router\index.js:277:22
at param (c:\MEAN_project\node_modules\express\lib\router\index.js:349:14)
at param (c:\MEAN_project\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (c:\MEAN_project\node_modules\express\lib\router\index.js:410:3)
TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (c:\MEAN_project\node_modules\express\lib\response.js:404:11)
at c:\MEAN_project\app\routes.js:25:17
at Layer.handle [as handle_request] (c:\MEAN_project\node_modules\express\lib\router\layer.js:95:5)
at next (c:\MEAN_project\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (c:\MEAN_project\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (c:\MEAN_project\node_modules\express\lib\router\layer.js:95:5)
at c:\MEAN_project\node_modules\express\lib\router\index.js:277:22
at param (c:\MEAN_project\node_modules\express\lib\router\index.js:349:14)
at param (c:\MEAN_project\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (c:\MEAN_project\node_modules\express\lib\router\index.js:410:3)
It says to use the absolute path or to specify the root, but I'm using the path starting from the project root so it seems like it should work. I was previously using sendfile()
and I was at least able to get basic html to render but I got the message that it was deprecated so I changed to using sendFile()
. I was working on bootstrapping the app so that html is injected from app.component.ts into index.html but that wasn't loading at all.
What path should I be using to specify the location of the index.html file or should I be using sendFile()
differently?