• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

joer717

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Node.js Web 模块

目前最主流的三个Web服务器是Apache、Nginx、IIS。

Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,

Web服务器的基本功能就是提供Web信息浏览服务。

它只需支持HTTP协议、HTML文档格式及URL,与客户端的网络浏览器配合。

大多数 web 服务器都支持服务端的脚本语言(php、python、ruby)等,

并通过脚本语言从数据库获取数据,将结果返回给客户端浏览器。

 

 

使用 Node 创建 Web 服务器

var http = require('http');

var fs = require('fs');

var url = require('url');

// 创建服务器

http.createServer( function (request, response) {

// 解析请求,包括文件名

var pathname = url.parse(request.url).pathname;

// 输出请求的文件名

console.log("Request for " + pathname + " received.");

// 从文件系统中读取请求的文件内容

fs.readFile(pathname.substr(1), function (err, data) {

if (err) {

console.log(err);

// HTTP 状态码: 404 : NOT FOUND

// Content Type: text/plain

response.writeHead(404, {'Content-Type': 'text/html'}); }

else{

// HTTP 状态码: 200 : OK

// Content Type: text/plain

response.writeHead(200, {'Content-Type': 'text/html'});

// 响应文件内容

response.write(data.toString());

}

// 发送响应数据

response.end();

});

}).listen(8080); // 控制台会输出以下信息

console.log('Server running at http://127.0.0.1:8080/');

 

posted on 2019-03-13 14:29  joer717  阅读(80)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3