在声明 HTTP 和 HTTPS REST 服务器时保持 DRY

Keep to DRY in declaring HTTP and HTTPS REST server

我正在使用 node.js Restify v4.0.3。 REST API 服务器支持 HTTP 和 HTTPS。目前,声明服务器的代码违反了 DRY(不要重复自己)原则。

HTTP 服务器的声明代码。

var server = restify.createServer({
    name: 'myapp',
    version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

server.use(restify.CORS());
server.opts(/.*/, function (req,res,next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", req.header("Access-Control-Request-Method"));
    res.header("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers"));
    res.send(200);
    return next();
});

server.listen(8400, function () {
    console.log('%s listening at %s', server.name, server.url);
});

HTTPS服务器声明码

var https_options = {
    name: 'myapp',
    version: '1.0.0',
    key: fs.readFileSync('./HTTPS.key'), //on current folder
    certificate: fs.readFileSync('./HTTPS.cert'),
};

var https_server = restify.createServer(https_options);

https_server.use(restify.acceptParser(server.acceptable));
https_server.use(restify.queryParser());
https_server.use(restify.bodyParser());

https_server.use(restify.CORS());
https_server.opts(/.*/, function (req,res,next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", req.header("Access-Control-Request-Method"));
    res.header("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers"));
    res.send(200);
    return next();
});

https_server.listen(8500, function () {
    console.log('%s listening at %s', https_server.name, https_server.url);
});

有相当多的重复。如何改进代码以符合DRY原则?

您可以将所有 .use 调用放在一个 setupDependencies 调用中,将所有 header 设置放在一个 setHeaders 调用中。

var https_options = {
    name: 'myapp',
    version: '1.0.0',
    key: fs.readFileSync('./HTTPS.key'), //on current folder
    certificate: fs.readFileSync('./HTTPS.cert'),
};

var https_server = restify.createServer(https_options);

https_server.use(restify.acceptParser(server.acceptable));
https_server.use(restify.queryParser());
https_server.use(restify.bodyParser());

https_server.use(restify.CORS());
https_server.opts(/.*/, function (req,res,next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", req.header("Access-Control-Request-Method"));
    res.header("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers"));
    res.send(200);
    return next();
});

会减少到:

var https_options = {
    name: 'myapp',
    version: '1.0.0',
    key: fs.readFileSync('./HTTPS.key'), //on current folder
    certificate: fs.readFileSync('./HTTPS.cert'),
};

var https_server = restify.createServer(https_options);

setupDependencies(https_server);

https_server.opts(/.*/, function (req,res,next) {
    setHeaders(req, res);
    res.send(200);
    return next();
});


/* helpers */

function setupDependencies(server) {
    server.use(restify.acceptParser(server.acceptable));
    server.use(restify.queryParser());
    server.use(restify.bodyParser());
    server.use(restify.CORS());
};

function setHeaders(req, res) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", req.header("Access-Control-Request-Method"));
    res.header("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers"));
};