12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import { Server } from 'node:http';
- const app = new Server();
- // app.on('connect', (request) => {
- // console.info('有客户端连接进来...', request);
- // });
- // app.on('close', () => {
- // console.info('有某个客户端断开连接...');
- // });
- app.on('request', (request, response) => {
- console.log(request);
- console.log('有请求进来...');
- // response.write('hello');
- // // response.end(); // end方法可以通知客户端响应结束
- // response.end(' world.');
- // response.setHeader('Content-Type', 'text/plain');
- // response.end('<div><strong>hello, node.js!</strong></div>');
- response.writeHead(200, 'yes', {
- 'Content-Type': 'application/json',
- });
- response.end(
- JSON.stringify({
- code: 1,
- data: [
- {
- id: 1,
- title: '吃饭',
- done: false,
- },
- ],
- msg: 'ok',
- })
- );
- });
- export function start(port = 8080, domain = 'localhost') {
- // 开启http服务,并监听所有连接
- app.listen(port, domain, () => {
- // 监听8080以及服务都启动成功
- console.log(`Server is running at port ${port}`);
- });
- }
|