我试图通过socket.io在带有expressjs的打字稿编写节点服务器中使用web-socket。捆绑了webpack。
服务器代码如下所示:
import * as Express from "express";
import * as SocketIO from "socket.io";
import * as Http from "http";
const app = Express();
app.get("/", (reqest: Express.Request, response: Express.Response) => {
response.sendFile(process.cwd() + "/dist/index.html");
});
app.use(Express.static("./dist/"));
const server = app.listen(3210, () => {
console.log("Listening to port 3210");
});
const io = SocketIO.listen(server);
最后一行导致后面提到的问题。
相应的webpack.config.js
:
module.exports = [
{
entry: "./server/main.ts",
output: {
path: "./dist",
filename: "server.js"
},
debug: true,
devtool: "source-map",
module: {
loaders: [
{
test: /\.ts$/,
loader: "ts-loader"
}
, {
test: /\.json/,
loader: "json-loader"
}
]
},
target: "node",
node: {
fs: "empty",
net: "empty"
}
ts: {
configFileName: 'server.tsconfig.json'
}
}
];
但最后webpack
打印出以下警告:
>警告在./~/express/lib/view.js 中关键依赖关系:
78:29-56依赖的请求是表达式
@ ./~/express/lib/view.js 78:29-56
>警告在./~/socket.io/~/engine.io/lib/server.js关键依赖关系:
75:43-65依赖的请求是表达式
@ ./~/socket.io/~/engine.io/lib/server.js 75:43-65
>警告在./~/socket.io/~/engine.io/~/ws/lib/Validation.js找不到模块:错误:无法在... \ node_modules \ socket.io \ node_modules \ engine.io \ node_modules \ ws \ lib
中解析模块'utf-8-validate'@ ./~/socket.io/~/engine.io/~/ws/lib/Validation.js 10:19-44
>警告在./~/socket.io/~/engine.io/~/ws/lib/BufferUtil.js找不到模块:错误:无法在... \ node_modules \ socket.io \ node_modules \ engine.io \ node_modules \ ws \ lib
中解析模块'bufferutil'@ ./~/socket.io/~/engine.io/~/ws/lib/BufferUtil.js 10:19-40
>警告在./~/socket.io/~/socket.io-client/~/engine.io-client/~/ws/lib/Validation.js找不到模块:错误:无法解析... \ node_modules \ socket.io \ node_modules \ socket.io-client \ node_modules \ engine.io-client \ node_modules \ ws \中的模块'utf-8-validate' LIB
@ ./~/socket.io/~/socket.io-client/~/engine.io-client/~/ws/lib/Validation.js 10:19-44
>警告在./~/socket.io/~/socket.io-client/~/engine.io-client/~/ws/lib/BufferUtil.js找不到模块:错误:无法在... \ node_modules \ socket.io \ node_modules \ socket.io-client \ node_modules \ engine.io-client \ node_modules \ ws \ lib
@ ./~/socket.io/~/socket.io-client/~/engine.io-client/~/ws/lib/BufferUtil.js 10:19-40
进程以代码0终止。
运行server.js会导致:
fs.js:549返回binding.open(pathModule._makeLong(path),stringToFlags(flags),mode);
TypeError:path必须是字符串
...
有谁知道我做错了什么?
答案 0 :(得分:0)
import * as SocketIO from "socket.io";
应该是:
import SocketIO from "socket.io-client";