Node.js HelloWorld的小细节
2017-10-01 11:00
585 查看
var http = require('http'); http.createServer(function(req,res){ res.writeHead(200,{'Content-Type' : 'text/html'}); res.write('<h1>Node.js</h1>'); console.log("running..."); res.end('<p>Hello World!!</p>'); }).listen(3000); console.log("HTTP server is listening at port 3000.");
运行结果发现每次访问,后台会出现两条running信息。
这是因为访问完之后,浏览器还会去访问
/favicon.ico,在实际调试过程中两次访问会给我们调试带来麻烦还有许多问题,所以我们要防止他进行第二次的访问。代码如下:
var http = require('http'); http.createServer(function(req,res){ res.writeHead(200,{'Content-Type' : 'text/html'}); if(req.url !== "/favicon.ico"){//清楚第二次访问 res.write('<h1>Node.js</h1>'); console.log("running..."); res.end('<p>Hello World!!</p>');//不写则没有http协议尾,但会产生两次访问 } }).listen(3000); console.log("HTTP server is listening at port 3000.");
这样每次访问,后台只会输出一条running信息了。
相关文章推荐
- nodejs hello world (调用文件执行没反应)
- nodejs express hello world
- 【NodeJS】-2-第一个node程序helloworld.js
- NodeJS Hello World
- Node.Js + express, run "hello world " in windows 8.1
- 【NodeJS】-2-第一个node程序helloworld.js
- NodeJS Hello world
- node.js hello world
- Node.js hello world
- Hyperledger fabric Client Node.js Hello World示例程序
- node.js helloworld
- node.js Hello world
- node.js 之 Hello,World in Node !
- node.js 学习笔记001 :Hello,world !
- 01. Node js Hello world
- Hello, world! (Node.js)
- Nodejs hello world
- Node.js 基础--00 Hello,World
- Serverless 框架 OpenWhisk 开发指南:使用 Node.js 编写 hello, world
- 【1】Node.js Hello World! 发布静态网页