__dirname在NodeJS SocketIO中未定义

时间:2019-06-03 08:30:45

标签: javascript node.js express

因此,我正在尝试使用nodejs(socketio)从头开始构建一个实时聊天应用程序。目前,我正在使用laravel框架,但是要继续我的项目,我也需要使用nodejs。这将帮助我通过使用nodejs套接字实时完成laravel项目,这就是我从头开始编写本教程的原因。刚开始学习这个。使用本教程

https://www.youtube.com/watch?v=tHbCkikFfDE

这是我的server.js代码

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
users = [];
connections = [];

server.listen(process.env.PORT || 3000);
console.log('Server running...');

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

发现错误

,当我尝试使用此代码node server运行服务器时,预期的浏览器将显示此错误

ReferenceError: _dirname is not defined
at C:\xampp\htdocs\iochat\server.js:12:18
at Layer.handle [as handle_request] (C:\xampp\htdocs\iochat\node_modules\express\lib\router\layer.js:95:5)
at next (C:\xampp\htdocs\iochat\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\xampp\htdocs\iochat\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\xampp\htdocs\iochat\node_modules\express\lib\router\layer.js:95:5)
at C:\xampp\htdocs\iochat\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\xampp\htdocs\iochat\node_modules\express\lib\router\index.js:335:12)
at next (C:\xampp\htdocs\iochat\node_modules\express\lib\router\index.js:275:10)
at expressInit (C:\xampp\htdocs\iochat\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (C:\xampp\htdocs\iochat\node_modules\express\lib\router\layer.js:95:5)

简而言之__dirname is not defined我该如何解决?

0 个答案:

没有答案