Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: path is not defined at Object.handle

js file,

app.get('/',function(req,res){
    res.sendfile(_dirname + '/index.html');
});

node.js can start ,but I got this error,

ReferenceError: path is not defined at Object.handle (E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\app.js:12:15) at next_layer (E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\router\route.js:103:13) at Route.dispatch (E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\router\route.js:107:5) at E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\router\index.js:195:24 at Function.proto.process_params (E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\router\index.js:251:12) at next (E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\router\index.js:189:19) at Layer.expressInit [as handle] (E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\middleware\init.js:23:5) at trim_prefix (E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\router\index.js:226:17) at E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\router\index.js:198:9 at Function.proto.process_params (E:\javaEEstudy\node.js\node.js入门经典\socket\socket.io_express\node_modules\express\lib\router\index.js:251:12)

the index.html and app.js is on the same file ,express version is 4.x

like image 614
Shany Chen Avatar asked Nov 19 '16 05:11

Shany Chen


1 Answers

First you should use

res.sendFile()

instead of

res.sendfile()

Second, the parameter of the above-mentioned method should be

__dirname+'index.html'

instead of

_dirname+'index.html'

In other words

app.get('/',function(req,res){
    res.sendFile(__dirname + '/index.html'); 
});

Also you have to declare path in your Node.js app:

var path = require("path");
like image 121
Hanjun Chen Avatar answered Sep 21 '22 11:09

Hanjun Chen