Node.js TODO

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);
    });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值