You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js v8.9.4出现Unexpected token =>语法错误求助

问题分析与解决办法

嘿,我一眼就发现问题出在哪啦!你把普通函数声明和箭头函数的语法混在一起用了,这才触发了SyntaxError: Unexpected token =>的错误——和你的Node.js版本完全没关系哦,v8.9.4是支持箭头函数的。

另外还有个小细节要注意:原生Node.js的http模块响应对象里没有res.send()方法,这个是Express框架的专属API,原生环境得用res.end()来结束响应并返回内容。

下面给你两种修正后的正确写法,选一种替换原代码就行:

写法一:使用普通函数

const http = require('http');
http.createServer(function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World');
}).listen(3000);
console.log('Server running at http://localhost:3000/');

写法二:使用箭头函数

const http = require('http');
http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World');
}).listen(3000);
console.log('Server running at http://localhost:3000/');

内容的提问来源于stack exchange,提问作者Justin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:32:57