http模块
<pre><code>// 引入http模块
const http = require('http');
/**
* 创建http服务
* request 获取url传过来的信息
* response 给浏览器响应的信息
*/
http.createServer((req, res) => {
if (req.url == "/") {
// 获取url
console.log(req.url)
// 设置请求头
res.writeHead(200, { 'Content-Type': 'text/html;charset="utf-8"' });//设置字符集文件编码,防止中文乱码
// 传给页面上的内容
const head = `<head><meta charset="UTF-8"></head>`//设置字符集文件编码,防止中文乱码
res.write(head)
res.write('你好 nodejs')
// 传给页面内容并结束响应
res.end();
}
if (req.url == "/api/getBannerData") {
// 获取url
console.log(req.url)
// 设置请求头
res.writeHead(200, { 'Content-Type': 'text/html;charset="utf-8"' });//设置字符集文件编码,防止中文乱码
// 传给页面上的内容
const head = `<head><meta charset="UTF-8"></head>`//设置字符集文件编码,防止中文乱码
res.write(head)
res.write('getBannerData')
// 传给页面内容并结束响应
res.end();
}
}).listen(3000); // 端口号
console.log('Server running at http://127.0.0.1:3000/');</code></pre>
<p><a href="https://www.hellojava.com/a/74971.html">https://www.hellojava.com/a/74971.html</a></p>