Node.js创建第一个应用

Node.js开发的目的就是为了用JavaScript编写Web服务器程序, 在使用Node.js时,不仅仅是在实现一个应用,同时还实现了整个HTTP服务器。在创建Node.js第一个"Hello, World!"应用前,需先了解下Node.js应用是由哪几部分组成的:

  1. 引入require模块: 使用require指令载入Node.js模块
  2. 创建服务器: 服务器可以监听客户端的请求,类似于Apache、Nginx等HTTP服务器
  3. 接收请求与响应请求: 客户端可以使用浏览器或终端发送HTTP请求,服务器接收请求后返回响应数据

HTTP协议

要理解Web服务器程序的工作原理,首先,要对HTTP协议有基本的了解HTTP协议简介

HTTP服务器

开发HTTP服务器程序, 从头处理TCP连接, 解析HTTP是不现实的. 这些工作实际上已经由Node.js自带的http模块帮我们完成了. 应用程序并不直接和HTTP协议打交道, 而是操作http模块提供的requestresponse对象.

request对象封装了HTTP请求,调用request对象的属性和方法就可以得到所有HTTP请求的信息;

response对象封装了HTTP响应,操作response对象的方法,就可以把HTTP响应返回给浏览器;

用Node.js实现一个HTTP服务器程序。首先实现一个最简单的Web程序hello.js,它对于所有请求,都返回Hello world!:

步骤一、引入require模块

const http = require('http');

步骤二、创建服务器

使用http.createServer()方法创建服务器,并使用listen方法绑定8080端口. 函数通过request, response参数来接收和响应数据

const http = require('http');

http.createServer(function (request, response) {

    // 发送 HTTP 头部
// HTTP 状态值: 200 : OK
// 内容类型: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'}); // 发送响应数据 "Hello World"
response.end('Hello World!\n');
}).listen(8080); // 终端打印如下信息
console.log('Server running at http://127.0.0.1:8080/');
'use strict';

// 导入http模块:
const http = require('http'); // 创建http server,并传入回调函数:
const server = http.createServer(function (request, response) {
// 回调函数接收request和response对象,
// 获得HTTP请求的method和url:
console.log(request.method + ': ' + request.url);
// 将HTTP响应200写入response, 同时设置Content-Type: text/html:
response.writeHead(200, {'Content-Type': 'text/html'});
// 将HTTP响应的HTML内容写入response:
response.end('<h1>Hello world!</h1>');
}); // 让服务器监听8080端口:
server.listen(8080); console.log('Server is running at http://127.0.0.1:8080/');
'use strict';

// 导入http模块:
const http = require('http'); // 创建http server,并传入回调函数:
const server = http.createServer((request, response) => {
// 回调函数接收request和response对象,
// 获得HTTP请求的method和url:
console.log(request.method + ': ' + request.url);
// 将HTTP响应200写入response, 同时设置Content-Type: text/html:
response.writeHead(200, {'Content-Type': 'text/html'});
// 将HTTP响应的HTML内容写入response:
response.end('<h1>Hello world!</h1>');
}); // 让服务器监听8080端口:
server.listen(8080); console.log('Server is running at http://127.0.0.1:8080/');

在命令行下运行该文件,可以看到以下输出:

$ node hello.js
Server is running at http://127.0.0.1:8080/

不要关闭命令提示符,直接打开浏览器输入http://localhost:8080,即可看到服务器响应的内容:

同时,在命令提示符窗口,可以看到程序打印的请求消息:

GET: /
GET: /favicon.ico

文件服务器

继续扩展上面的Web程序, 可以设定一个目录,然后让Web程序变成一个文件服务器。要实现这一点,只需解析request.url中的路径,然后在本地找到对应的文件,把文件内容发送出去就可以了。

解析URL需要用到Node.js提供的url模块,它使用起来非常简单,通过parse()将一个字符串解析为一个Url对象:

'use strict';

const url = require('url');

console.log(url.parse('http://user:pass@host.com:8080/path/to/file?query=string#hash'));

结果如下:

Url {
protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host.com:8080',
port: '8080',
hostname: 'host.com',
hash: '#hash',
search: '?query=string',
query: 'query=string',
pathname: '/path/to/file',
path: '/path/to/file?query=string',
href: 'http://user:pass@host.com:8080/path/to/file?query=string#hash'
}

处理本地文件目录需要使用Node.js提供的path模块,它可以方便地构造目录:

'use strict';

const path = require('path');

// 解析当前目录
const workDir = path.resolve('.'); // 组合完整的文件路径:当前目录 + 'static' + 'index.html'
const filePath = path.join(workDir, 'static', 'index.html'); console.log(workDir);
console.log(filePath);
// D:\NodejsApp\middleware\workdir
// D:\NodejsApp\middleware\workdir\static\index.html

使用path模块可以正确处理操作系统相关的文件路径。在Windows系统下,返回的路径类似于C:\Users\Unity\static\index.html,这样,就不必关心怎么拼接路径了。最后,实现文件服务器file_server.js

'use strict';

const fs = require('fs');
const url = require('url');
const path = require('path');
const http = require('http'); // 从命令行参数获取root目录,默认是当前目录
const root = path.resolve(process.argv[2] || '.'); console.log(`Stctic root dir: ${root}`); // 创建服务器
http.createServer((request, response) => {
// 获得URL的path,类似/css/bootstrap.css
const pathname = url.parse(request.url).pathname; // 获得对应的本地文件路径,类似/src/css/bootstrap.css
const filepath = path.join(root, pathname); // 获取文件状态
fs.stat(filepath, (err, stats) => {
if (!err && stats.isFile()) {
// 没有出错并且文件存在
console.log('200' + request.url);
// 发送200响应
response.writeHead(200);
// 将文件流导向response
fs.createReadStream(filepath).pipe(response);
} else {
// 出错或文件不存在
console.log('404' + request.url);
// 发送404
response.writeHead(404);
response.end('404 Not Found');
}
});
}).listen(8080); console.log('Server is running at http://127.0.0.1:8080/');

没有必要手动读取文件内容, 由于response对象本身是一个Writable Stream,直接用pipe()方法就实现了自动读取文件内容并输出到HTTP响应

在命令行运行node file_server.js 然后在浏览器中输入http://localhost:8080/index.html

只要当前目录下存在文件index.html,服务器就可以把文件内容发送给浏览器。观察控制台输出

200 /index.html
200 /css/uikit.min.css
200 /js/jquery.min.js
200 /fonts/fontawesome-webfont.woff2

第一个请求是浏览器请求index.html页面,后续请求是浏览器解析HTML后发送的其它资源请求

练习

在浏览器输入http://localhost:8080/时,会返回404,原因是程序识别出HTTP请求的不是文件,而是目录。请修改file_server.js,如果遇到请求的路径是目录,则自动在目录下依次搜索index.htmldefault.html,如果找到了,就返回HTML文件的内容。

参考源码

http服务器代码(含静态网站)

Create First Application的更多相关文章

  1. kivy Create an application

    http://kivy.org/docs/guide/basic.html#quickstart I followed this tutorial about how to create basic ...

  2. maven command to create your application

    How do I make my first Maven project? We are going to jump headlong into creating your first Maven p ...

  3. Java SE series:1. environment configure and Hello world! [We use compiler and packager to create an application!]

    1. cli (command line interface) and gui (graphic user interface) use javahome path, search classpath ...

  4. [NativeScript] Create new application and run emulator

    Install: npm i -g nativescript Create: tns create <app_name> --ng Run: tns emulate ios List al ...

  5. How do I create an IIS application and application pool using InnoSetup script

    Create an IIS application. Create a new IIS application pool and set it's .NET version to 4. Set the ...

  6. cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析

    首先,是个敲代码的,基本上都知道程序的入口是main函数,显然,就算在cocos2d-x框架中也一样 我们看看main函数做了什么 #include "main.h" #inclu ...

  7. 应用程序域(Application Domain)

    应用程序域为隔离正在运行的应用程序提供了一种灵活而安全的方法. 应用程序域通常由运行时宿主创建和操作. 有时,您可能希望应用程序以编程方式与应用程序域交互,例如想在不停止应用程序运行的情况下卸载某个组 ...

  8. 【转】How to view word document in WPF application

    How to view word document in WPF application (CSVSTOViewWordInWPF) Introduction The Sample demonstra ...

  9. Application Pool Identities

    Whether you are running your site on your own server or in the cloud, security must be at the top of ...

随机推荐

  1. vue项目中上拉加载和下拉刷新页面的实现

    功能:上拉加载,下拉刷新 使用方法: 自己创建一个.vue的文件(我自己是创建了一个PullToRefresh.vue的文件),将代码粘贴进去,具体的样式问题自己在该文件中调整. <templa ...

  2. 15个Nodejs应用场景

    15个Nodejs应用场景 我们已经对Nodejs有了初步的了解,接下来看看Nodejs的应用场景. 2.1 Web开发:Express + EJS + Mongoose/MySQL express  ...

  3. Get史上最优雅的加密方式!没有之一!

    你的配置文件是不是还在使用下面这种落后的配置暴露一些密码: jdbc.url=jdbc:mysql://127.0.0.1:3305/afei jdbc.username=afei 如果是,那么继续往 ...

  4. JAVA_WEB--jsp语法

    JSP声明 一个声明语句可以声明一个或多个变量.方法,供后面的Java代码使用.在JSP文件中,必须先声明这些变量和方法然后才能使用它们. JSP声明的语法格式: <%! declaration ...

  5. postman(动态数据获取)

    一:返回报文为 json 格式 示例:因为充值记录接口中需要用到登录接口返回报文中的信息如下 1.以获取token(JWT)和uid为例 2.在登录接口的tests中写入代码(因为登录接口报文信息中有 ...

  6. WCF学习(二)

    WCF通道模型 绑定的本质是一个配置好的通道栈,为了方便程序员专著与业务逻辑,WCF提高了一系列常用绑定.随后会有相应的自定义通道栈代码 无论交互的另一方具体位置在哪里,WCF都会为消息的发送和接收建 ...

  7. Spring官网阅读(三)自动注入

    上篇文章我们已经学习了1.4小结中关于依赖注入跟方法注入的内容.这篇文章我们继续学习这结中的其他内容,顺便解决下我们上篇文章留下来的一个问题-----注入模型. 文章目录 前言: 自动注入: 自动注入 ...

  8. 随笔 - B树算法实现

    写代码之前,再回顾一下B树是什么,满足什么样的规则 B树规则: 排序方式:所有节点关键字是按递增次序排列,并遵循左小右大原则 子节点数:非叶节点的子节点数>1,且<=M ,且M>=2 ...

  9. Nacos系列(一):Nacos环境安装及Hello World示例

    现在微服务架构越来越火,微服务架构中比较重要的一项就是配置中心, Nacos是阿里巴巴的一个开源项目,它的其中一个功能就是可以作为配置中心,实现配置的动态变更.历史版本对比.配置回滚等功能. 更多的描 ...

  10. 【Hadoop离线基础总结】HDFS入门介绍

    HDFS入门介绍 概述 HDFS全称为Hadoop Distribute File System,也就是Hadoop分布式文件系统,是Hadoop的核心组件之一. 分布式文件系统是横跨在多台计算机上的 ...