12345678910111213141516171819202122 |
- import http from 'http';
- import url from 'url';
- import querystring from 'querystring';
- // 创建 HTTP 服务器
- http.createServer((req, res) => {
- res.setHeader('Access-Control-Allow-Origin', '*'); // 允许跨域访问
- if (req.method.toLowerCase() === 'get') {
- const mGet = url.parse(req.url, true);
- const username = mGet.query.username;
- const age = mGet.query.age;
- // 打印用户数据
- console.log(`用户名: ${username}, 年龄: ${age}`);
- // 返回响应
- res.writeHead(200, { 'Content-Type': 'text/plain' });
- res.end(`数据接收成功:用户名 = ${username}, 年龄 = ${age}`);
- } else {
- res.writeHead(404, { 'Content-Type': 'text/plain' });
- res.end('Not Found');
- }
- }).listen(3000, () => {
- console.log('Server running at http://localhost:3000/');});
|