Node.js Express开发指南:构建动态Web应用
1. 启动服务器
在使用 Node.js 和 Express 搭建服务器时,首先要明确如何启动和管理服务器。 app.listen(port, function(){ console.log ("Server ready!")}); 这行代码的作用是让服务器监听指定端口,若服务器启动成功,终端会显示 “Server ready!” 消息。
启动服务器的步骤如下:
1. 在终端输入 node index.js 。
2. 当 “Server ready!” 消息出现,表明服务器已在端口 3000 上运行。
若对 index.js 文件进行了修改,需停止并重启服务器使更改生效。停止服务器按 Ctrl+C ,重启则再次输入 node index.js 。若未看到 “Server ready!” 消息,要确保 index.js 文件中包含 const port = 3000; ,因为未指定端口服务器无法启动。另外,通常还会有 const host = "servername"; 这行代码,其中 servername 是服务器名称。若未定义主机,Express 默认使用 localhost ,即当前工作的计算机。在搭建实时服务器时,需使用实际的 Web 服务器名称。
超级会员免费看
订阅专栏 解锁全文
1333

被折叠的 条评论
为什么被折叠?



