Node.js TODO

巴帅
2023-12-01

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);
    });
}
 类似资料:

相关阅读

相关文章

相关问答