MicroHTTP服务器
一个小的、轻量级的、零依赖的Node.js内置的包装器 http 模块,用于快速定义路由处理程序。
安装
npm install @tobiasgjerstrup/microhttp-server
使用
import createApp from '@tobiasgjerstrup/microhttp-server';
const app = createApp();
app.get('/', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, world!');
});
app.listen(3000, '127.0.0.1', () => {
console.log('Listening on http://127.0.0.1:3000');
});
API
createApp()
返回一个具有以下方法的应用实例:
app.get(path, handler)
app.post(path, handler)
app.put(path, handler)
app.delete(path, handler)
为给定的HTTP方法和路径注册一个路由处理程序。
path-要匹配的精确URL路径(例如:"/users")handler(req, res)-标准Node.jshttp.IncomingMessage/http.ServerResponse处理程序
app.listen(port, callback)
app.listen(port, hostname, callback)
在给定的端口和可选主机名上启动HTTP服务器。
不匹配的路由响应为 404 Not Found.
许可证
国际安全委员会