CNode

nodejs 报这个错是什么原因?

问答
Hhujunqun发布于10 年前最后回复10 年前7 回复5661 浏览0 收藏

报错信息: const server = http.createServer((req, res)= >{ ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:902:3

代码: const http = require('http');

const hostname = '127.0.0.1'; const port = 3000;

const server = http.createServer((req, res)= >{ res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); });

server.listen(port, hostname, () => { console.log(Server running at http://${hostname}:${port}/); });

查看回复

回复 (7)

Z
zhangzeyang#1·10 年前

试试严格模式

From Noder

A
Alexis374#2·10 年前

中间有个空格。。。。。。

W
wssgcg1213#3·10 年前

空格 From Noder

Y
yinxin630#4·10 年前

=> 不是 = >

K
kelvv#5·10 年前

=和>中间的空格

H
hujunqun#6·10 年前

但我中间没加空格,在windows 下没报错,在ubuntu 下报这个错的

Z
zhangjh#7·10 年前

node版本不支持函数表达式吧,换成这样写法吧

const server = http.createServer(**function(req,res)**{
res.statusCode = 200;
res.setHeader(‘Content-Type’, ‘text/plain’);
res.end(‘Hello World\n’);
});
参与回复
登录后即可参与回复。登录