Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

mozai126/demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

8 Commits

Repository files navigation

demo

tui song ben di xiang mu dao git const http = require('http') const fs = require('fs') const url = require('url') http.createServer((req, res) => { let pathObj = url.parse(req.url, true) switch (pathObj.pathname) { case '/getWeather': if(pathObj.query.city === 'beijing') res.end(JSON.stringify({city: 'beijing', weather: 'sunny'})) else res.end(JSON.stringify({city: pathObj.query.city, weather: 'unknown'})) break default: try { let pathname = pathObj.pathname === '/' ? '/index.html' : pathObj.pathname res.end( fs.readFileSync(__dirname + pathname) ) } catch(e) { res.writeHead(404, 'Not Found') res.end('

404 Not Found~

') }
} }).listen(8080) 创建一个虚拟服务器监听8080端口,服务器收到请求后分析请求中的参数,如果请求参数(‘/’后的内容)为有获取天气字段,在进行判断对象中有city=对象,则返回数据给浏览器相关对象,否则判断是文件还是路径返回读取的内容, 如果报错了返回404

About

tui song ben di xiang mu dao git

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /