web前端全系列 教程
1839个小节阅读:2244.3k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
我们要为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码。
xxxxxxxxxx
const http = require('http')
const url = require('url')
const server = http.createServer(function (req, res) {
//获取请求路径
const pathname = url.parse(req.url).pathname;
if (pathname == '/') {
res.end('hello')
}else if (pathname == '/getList') {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.write(JSON.stringify({ recrods: [{ name: 'fyx' }] }))
res.end()
}else{
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('默认值')
}
})
server.listen('3030', function () {
console.log('服务器正在监听3030端口')
})
整理
xxxxxxxxxx
//router.js
module.exports=(pathname,res)=>{
if (pathname == '/') {
res.end('hello')
}else if (pathname == '/getList') {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
res.write(JSON.stringify({ recrods: [{ name: 'fyx' }] }))
res.end()
}else{
res.end('默认值')
}
}
xxxxxxxxxx
//test.js
const http = require('http')
const url = require('url')
const router = require('./router')
const server = http.createServer(function (req, res) {
const pathname = url.parse(req.url).pathname;
router(pathname,res)
})
server.listen('3030', function () {
console.log('服务器正在监听3030端口')
})
实时效果反馈
1.下列描述错误的是:
A Node.js路由指的是根据不同的请求路径做出不同的处理跟响应
B router
是NodeJs内置的模块
答案
1=>B