美文网首页
使用node创建简单服务器

使用node创建简单服务器

作者: 菲龍探雲 | 来源:发表于2016-11-11 14:44 被阅读13次
//开启严格模式
    "use strict";
//创建一个HTTP服务器
var http = require("http");
//创建一个服务
var server = http.createServer(function (request, response) {
    console.log(request.url);
    //处理请求和响应
    response.writeHead(200, {
        "Content-Type": "textml",//告诉客户端我给你的是HTML
        "key1": "value1"
    })
    //往响应体中放数据
    response.write('<head><meta charset="utf-8"/></head>')//解决中文乱码
    response.write("<h1>你好</h1>");
    response.end();//结束
});
//启动服务,监听8080端口
server.listen(8080, function (error) {
    console.log("成功监听8080")
});

成功监听之后我们可以看到

Paste_Image.png

打开浏览器输入http://localhost:8080/ 我们可以看到你好

Paste_Image.png

我们还可以看到请求的地址(浏览器默认会请求网站的图标)

Paste_Image.png

相关文章

网友评论

      本文标题:使用node创建简单服务器

      本文链接:https://www.haomeiwen.com/subject/wcjfpttx.html