nodejs中创建web服务被忽略的坑---listen hostname
2015-11-04 12:16
651 查看
nodejs中创建web服务时,习惯看官网例子
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at 官网的API上说到:' target='_blank'>http://127.0.0.1:1337/');官网的API上说到:
server.listen(port, [hostname], [backlog], [callback])#
Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
里面说到如果忽略了hostname,那么服务器将会接受所有IPV4地址的链接,IPv4地址包括127.0.0.1 localhost和本地IP。没有认真看API,以后要注意。那么这样做就可以实现监听本地IP、localhost、127.0.0.1了:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337);
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at 官网的API上说到:' target='_blank'>http://127.0.0.1:1337/');官网的API上说到:
server.listen(port, [hostname], [backlog], [callback])#
Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
里面说到如果忽略了hostname,那么服务器将会接受所有IPV4地址的链接,IPv4地址包括127.0.0.1 localhost和本地IP。没有认真看API,以后要注意。那么这样做就可以实现监听本地IP、localhost、127.0.0.1了:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337);
相关文章推荐
- java-WEB中的监听器Lisener
- GUI - Web前端开发框架
- 学习 Linux 是我们的爱情语言
- Extjs4.0 最新最全视频教程
- 使用 HTTP 上传 G 级的文件之 Node.js 版本
- Node.js压缩web项目中的js,css和图片
- MyEclipse Web Project转Eclipse Dynamic Web Project
- axis备忘
- 基础命令学习总结
- 异步流程控制:7 行代码学会 co 模块
- [译] React 入门
- 新注册
- Prototype 学习 工具函数学习($方法)
- Erlang实现的一个Web服务器代码实例
- 防止网页脚本病毒执行的方法-from web
- 自学成才的秘密:115个 web Develop 资源
- 四大漏洞入侵博客
- 使用批处理修改web打印设置笔记 适用于IE