返回列表

四步搞定restify

默认分类 2014/04/10 19:32

'use strict';
var restify = require('restify');

global.api = restify.createServer({
    name: 'myapp',
    version: '1.0.0'
});

api.use(restify.acceptParser(api.acceptable));
api.use(restify.queryParser());
api.use(restify.bodyParser());

// Load router list
api.get('/hello', function (req, res, next) {res.send('Hello world.'); return next();});

api.get('/test/:name', function (req, res, next) {
    var body = '<html><body><h1>hello '+req.params.name+'</h1><p>This is my first artical!<br/><input type="button" text="Click Me" onclick="alert(\'hello\')" /></p></body></html>';
    res.writeHead(200, {
        //'Content-Length': Buffer.byteLength(body),
        'Content-Type': 'text/html'
    });
    res.write(body);
    res.end();
    return next();
});

// Static resource
api.get(/^\/((.*)(\.)(.+))*$/, restify.serveStatic({ directory: 'public', default: 'index.html' }));

// Start server
api.listen('7001', function () {
    console.log('%s listening at %s', api.name, api.url);
});