Node.js TODO
来源:互联网 发布:js中的offsettop 编辑:程序博客网 时间:2024/04/30 06:30
Node.js TODO
var http = require('http');var url = require('url');var items = [];http.createServer(function(req, res) { console.log(req.method); switch (req.method) { case 'POST': var item = ''; req.setEncoding('utf8'); req.on('data', function(chunk) { item += chunk; }); req.on('end', function() { console.log(item); items.push(item); res.end('Ok\n'); }); break; case 'GET': items.forEach(function(item, i) { res.write(i + ') ' + item + '\n'); }); res.end(); break; case 'DELETE': console.log(url.parse(req.url)); var path = url.parse(req.url).pathname; var i = parseInt(path.slice(1)); if (isNaN(i)) { res.writeHead(400, {'Content-Type': 'text/plain'}); res.end('Invalid item id'); } else if (!items[i]) { res.writeHead(404, {'Content-Type': 'text/plain'}); res.end('Item not found'); } else { items.splice(i, 1); res.write('Ok\n'); items.forEach(function(item, i) { res.write(i + ') ' + item + '\n'); }); res.end(); } break; }}).listen(3000);// GET//curl.exe http://localhost:3000//// POST//curl.exe -d '111' http://localhost:3000//// DELETE//curl.exe -X "DELETE" http://localhost:3000/1
var http = require('http');var querystring = require('querystring');var items = [];var server = http.createServer(function(req, res) { console.log(req.method); if ('/' == req.url) { switch (req.method) { case 'GET': show(res); break; case 'POST': console.log('xxxx'); add(req, res); break; default: badRequest(res); } } else { notFound(res); }}).listen(3000);function show(res) { var html = '<html><head><title>Todo List</title></head><body>' + '<h1>Todo List</h1>' + '<ul>' + items.map(function(item) { return '<li>' + item + '</li>' }).join('') + '</ul>' + '<form method="post" action="/">' + '<p><input type="text" name="item" /></p>' + '<p><input type="submit" value="Add Item" /></p>' + '</form>' + '</body></html>'; res.setHeader('Content-Type', 'text/html'); res.setHeader('Content-Length', Buffer.byteLength(html)); res.end(html);}function notFound(res) { res.statusCode = 404; res.setHeader('Content-Type', 'text/plain'); res.end('Not Found');}function badRequest(res) { res.statusCode = 400; res.setHeader('Content-Type', 'text/plain'); res.end('Bad Request');}function add(req, res) { console.log('add'); var body = ''; req.setEncoding('utf8'); req.on('data', function(chunk) { body += chunk; }) req.on('end', function() { var obj = querystring.parse(body); items.push(obj.item); show(res); });}
1 0
- Node.js TODO
- TODO:Node.js pm2使用方法
- TODO:Ubuntu下安装Node
- todo
- todo
- todo
- Todo
- [TODO]
- TODO
- todo
- todo
- TODO
- //TODO
- ToDo
- TODO
- TODO
- TODO
- todo
- 链表
- [Android实例] 波浪动画效果,正弦曲线绘制
- Zoho Survey功能更新
- AFNetwork 3.0 源码解读(四)AFURLResponseSerialization
- linux内核开发(一)linux体系结构与内核结构
- Node.js TODO
- linux下编译,运行java文件
- 如何编写Android.mk 文件
- Local Notification not working since updating to IOS 8 and Xcode 6
- HttpClient使用详解
- ubuntu 网络设置
- javascript语法
- Map集合没有迭代器,以下是Map的两种取出方式:
- google的guice入门详解