安装nodejs、npm
安装成功之后,使用命令测试是否成功:
|
|
|
|
初始化项目配置
在chat
文件夹下执行初始化命令来获取package.json文件,如果你自己能记住也是可以手写的
|
|
web服务需要引用的模块
http模块(http协议)
|
|
|
|
Node中提供了http模块,其中封装了高效的http服务器和http客户端
http.server是一个基于事件的HTTP服务器,内部是由c++实现的,接口由JavaScript封装
http.request是一个HTTP客户端工具。用户向服务器发送数据。
url模块(url解析)
|
|
|
|
This module has utilities for URL resolution and parsing meant to have feature parity with node.js core url module.
fs模块(文件系统)
|
|
|
|
Node.js 文件系统封装在 fs 模块是中,它提供了文件的读取、写入、更名、删除、遍历目录、链接等POSIX 文件系统操作。
与其他模块不同的是,fs 模块中所有的操作都提供了异步的和 同步的两个版本,例如读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFileSync()。我们以几个函数为代表,介绍 fs 常用的功能,并列出 fs 所有函数 的定义和功能。
path模块(路径解析)
|
|
|
|
This is an exact copy of the NodeJS ’path’ module published to the NPM registry.
构建一个基于nodejs的web服务器
新建一个index.html
|
|
新建一个webserver.js
|
|
安装用到的模块到本地项目中:
|
|
安装成功之后执行命令
|
|
在浏览器起中打开:http://127.0.0.1:8081/ 即可查看效果:

Express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
express API地址:http://www.expressjs.com.cn/4x/api.html
创建一个文件夹express+nodejs
,执行初始化项目操作,在项目上安装express
|
|
并且新建index.js
、index.html
文件
|
|
|
|
在命令行中执行:
如果提示listen EADDRINUSE :::3000
就说明端口被占用了,可以换成其他端口
|
|
在浏览器中打开 http://localhost:3000/

使用Express加载模版并输出数据
未完待续……