0

I have the following server.js file:

var express = require('express'),
    api     = require('./api'),
    app     = express();

app
    .use(express.static('./public')) 
    .use('./api', api)
    .get('*', function (req, res) { 
        res.sendfile('public/main.html'); 
    })
    .listen(3000); 

and the server.js file is located in C:\Users\myName\Desktop\prodfixes\server.js and main.html is located in C:\Users\myName\Desktop\prodfixes\public\main.html and going to http://localhost:3000/ doesn't throw an error on screen however on the command line (I am using nodemon server.js to run the server) I am getting the error: express deprecated res.sendfile: Use res.sendFile instead. So then I obviously changed res.sendfile to res.sendFile but then I get an error when i refresh the screen:

TypeError: path must be absolute or specify root to res.sendFile at ServerResponse.sendFile (C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\response.js:394:11) at C:\Users\myName\Desktop\prodfixes\server.js:9:7 at Layer.handle [as handle_request] (C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\router\layer.js:95:5) at C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\router\index.js:277:22 at param (C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\router\index.js:349:14) at param (C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\router\index.js:365:14) at Function.process_params (C:\Users\myName\Desktop\prodfixes\node_modules\express\lib\router\index.js:410:3)

Sorry I am new to angular and using servers so any help would be greatly appreciated. Thanks.

userk
  • 1
  • 2
  • The error sounds like you are using the wrong path. Make sure it is correct. Try different potential paths till you can find some resource. Have you read up the documentation for sendFile? It may require a different path than the one you were using before. – S. Buda Aug 17 '15 at 22:59

2 Answers2

1

Using path.resolve should resolve this issue in a cross-platform compatible way:

var express = require('express'),
    path = require('path'),
    api = require('./api'),
    app = express();

app
    .use(express.static('./public')) 
    .use('./api', api)
    .get('*', function (req, res) { 
        res.sendFile(path.resolve('public/main.html')); 
    })
    .listen(3000); 

path.resolve will provide the absolute path to the file which will satisfy the requirements for the res.sendFile() function:

Unless the root option is set in the options object, path must be an absolute path of the file.

http://expressjs.com/api.html

oznu
  • 1,604
  • 2
  • 12
  • 11
0

I agree with S. Buda... definitely seems like you have the wrong path. The error specifies "TypeError: path must be absolute or specify root to res.sendFile at ServerResponse.sendFile" - try using the full path instead of the relative path. C:\Users\myName\Desktop\prodfixes\public\main.html instead of just public/main.html.

jadams
  • 195
  • 1
  • 8