工具
- node-dev
- ts-node
- ts-node-dev
新建项目
- yarn init -y
- 新建 index.ts
- 使用命令行或WebStorm启动
$ yarn add --dev @types/node安装node声明文件
- 引入http模块
- 用http创建server
- 监听server的request事件(可简写)
server.listen(8888)
开始监听8888端口使用
curl -v http://localhost:8888
发请求根据url返回不同的文件
```typescript import as http from ‘http’; import { IncomingMessage, ServerResponse } from ‘http’; import as fs from ‘fs’; import * as p from ‘path’ const server = http.createServer() const publicDir = p.resolve(__dirname, ‘public’) server.on(‘request’, (request: IncomingMessage, response: ServerResponse) => { const { method, url, headers } = request
switch (url) {
case '/index.html':
response.setHeader('Content-Type', 'text/html;charset=utf-8')
fs.readFile(p.resolve(publicDir, 'index.html'), (error, data) => {
if (error) throw error;
response.end(data.toString())
})
break;
case '/style.css':
response.setHeader('Content-Type', 'text/css;charset=utf-8')
fs.readFile(p.resolve(publicDir, 'style.css'), (error, data) => {
if (error) throw error;
response.end(data.toString())
})
break;
case '/main.js':
response.setHeader('Content-Type', 'text/javascript;charset=utf-8')
fs.readFile(p.resolve(publicDir, 'main.js'), (error, data) => {
if (error) throw error;
response.end(data.toString())
})
break;
default:
response.end('no file')
break;
} })
server.listen(8888)
<a name="rDu4q"></a>
# 处理查询参数
**_url.parse_**
```typescript
import * as http from 'http';
import { IncomingMessage, ServerResponse } from 'http';
import * as fs from 'fs';
import * as p from 'path';
import * as url from 'url';
const server = http.createServer()
const publicDir = p.resolve(__dirname, 'public')
server.on('request', (request: IncomingMessage, response: ServerResponse) => {
const { method, url: path, headers } = request
const { pathname, search } = url.parse(path)
console.log(pathname, search)
switch (pathname) {
case '/index.html':
response.setHeader('Content-Type', 'text/html;charset=utf-8')
fs.readFile(p.resolve(publicDir, 'index.html'), (error, data) => {
if (error) throw error;
response.end(data.toString())
})
break;
case '/style.css':
response.setHeader('Content-Type', 'text/css;charset=utf-8')
fs.readFile(p.resolve(publicDir, 'style.css'), (error, data) => {
if (error) throw error;
response.end(data.toString())
})
break;
case '/main.js':
response.setHeader('Content-Type', 'text/javascript;charset=utf-8')
fs.readFile(p.resolve(publicDir, 'main.js'), (error, data) => {
if (error) throw error;
response.end(data.toString())
})
break;
default:
response.end('no file')
break;
}
})
server.listen(8888)
匹配任意文件
import * as http from 'http';
import { IncomingMessage, ServerResponse } from 'http';
import * as fs from 'fs';
import * as p from 'path';
import * as url from 'url';
const server = http.createServer()
const publicDir = p.resolve(__dirname, 'public')
server.on('request', (request: IncomingMessage, response: ServerResponse) => {
const { method, url: path, headers } = request
const { pathname, search } = url.parse(path)
console.log(pathname, search)
// response.setHeader('Content-Type', 'text/html;charset=utf-8')
const filename = pathname.substr(1)
fs.readFile(p.resolve(publicDir, filename), (error, data) => {
if (error) {
response.statusCode = 404
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('你要的文件不存在')
} else {
response.end(data)
}
})
})
server.listen(8888)
处理不存在的文件
返回一个404页面
import * as http from 'http';
import { IncomingMessage, ServerResponse } from 'http';
import * as fs from 'fs';
import * as p from 'path';
import * as url from 'url';
const server = http.createServer()
const publicDir = p.resolve(__dirname, 'public')
server.on('request', (request: IncomingMessage, response: ServerResponse) => {
const { method, url: path, headers } = request
const { pathname, search } = url.parse(path)
console.log(pathname, search)
// response.setHeader('Content-Type', 'text/html;charset=utf-8')
let filename = pathname.substr(1)
if (filename === '') { filename = 'index.html' }
fs.readFile(p.resolve(publicDir, filename), (error, data) => {
if (error) {
if (error.errno === -4058) {
response.statusCode = 404
response.setHeader('Content-Type', 'text/html;charset=utf-8')
fs.readFile(p.resolve(publicDir, '404.html'), (error, data) => {
response.end(data)
})
} else if (error.errno === -4068) {
response.statusCode = 403
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('无权限访问目录')
} else {
response.statusCode = 500
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('服务器有问题')
}
} else {
response.end(data)
}
})
})
server.listen(8888)
处理非GET请求
405 Method Not Allowed
import * as http from 'http';
import { IncomingMessage, ServerResponse } from 'http';
import * as fs from 'fs';
import * as p from 'path';
import * as url from 'url';
const server = http.createServer()
const publicDir = p.resolve(__dirname, 'public')
server.on('request', (request: IncomingMessage, response: ServerResponse) => {
const { method, url: path, headers } = request
if (method !== 'GET') {
response.statusCode = 405
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('不支持非GET请求')
return
}
const { pathname, search } = url.parse(path)
console.log(pathname, search)
// response.setHeader('Content-Type', 'text/html;charset=utf-8')
let filename = pathname.substr(1)
if (filename === '') { filename = 'index.html' }
fs.readFile(p.resolve(publicDir, filename), (error, data) => {
if (error) {
if (error.errno === -4058) {
response.statusCode = 404
response.setHeader('Content-Type', 'text/html;charset=utf-8')
fs.readFile(p.resolve(publicDir, '404.html'), (error, data) => {
response.end(data)
})
} else if (error.errno === -4068) {
response.statusCode = 403
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('无权限访问目录')
} else {
response.statusCode = 500
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('服务器有问题')
}
} else {
response.end(data)
}
})
})
server.listen(8888)
添加缓存选项
import * as http from 'http';
import { IncomingMessage, ServerResponse } from 'http';
import * as fs from 'fs';
import * as p from 'path';
import * as url from 'url';
const server = http.createServer()
const publicDir = p.resolve(__dirname, 'public')
server.on('request', (request: IncomingMessage, response: ServerResponse) => {
const { method, url: path, headers } = request
if (method !== 'GET') {
response.statusCode = 405
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('不支持非GET请求')
return
}
const { pathname, search } = url.parse(path)
console.log(pathname, search)
// response.setHeader('Content-Type', 'text/html;charset=utf-8')
let filename = pathname.substr(1)
if (filename === '') { filename = 'index.html' }
fs.readFile(p.resolve(publicDir, filename), (error, data) => {
if (error) {
if (error.errno === -4058) {
response.statusCode = 404
response.setHeader('Content-Type', 'text/html;charset=utf-8')
fs.readFile(p.resolve(publicDir, '404.html'), (error, data) => {
response.end(data)
})
} else if (error.errno === -4068) {
response.statusCode = 403
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('无权限访问目录')
} else {
response.statusCode = 500
response.setHeader('Content-Type', 'text/html;charset=utf-8')
response.end('服务器有问题')
}
} else {
//返回文件内容
response.setHeader('Cache-Control', 'public,max-age=30000000')
response.end(data)
}
})
})
server.listen(8888)