node.js - 使用 socket.io 发送标头后无法设置标头
问题描述
我是 nodeJs 的新手。我正在使用icket.io 收到此错误“发送后无法设置标头”
这是我的代码
var app = require('express')();
var http = require('http').createServer(app);
app.get('/', (req, res) => {
res.send('<h1>Hello world</h1>');
res.sendFile(__dirname + '/socket.html');
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
这是我的 html 文件
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: 0.5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>
解决方案
你要么打电话,res.send
要么res.sendFile
不打电话:
app.get('/', (req, res) => {
res.send('<h1>Hello world</h1>');
});
或者
app.get('/', (req, res) => {
res.sendFile(__dirname + '/socket.html');
});
推荐阅读
- java - Akka:在询问时丢失了`child.path.name`的参考
- javascript - 在 vue.js 中访问数组对象属性
- java - 在 Java 中输入来自用户的数组中的整数,用空格分隔
- regex - 如何在 Perl 中使用正则表达式来查看变量是否只有特定字符?
- windows - Windows VM 上的 IOmeter v/s FIO
- angular - 如何使用 Angular 5 验证网站地址
- sql - 我可以在另一个 WITH 表 AS 中使用 WITH 表 AS 吗?
- android - onActivityResult() 后无法更新列表视图;
- php - array_combine 结果只有一个结果
- c# - C#如何确保两个对象之间的自动双链接