gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:22:32 24 4
gpt4 key购买 nike

我使用的是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"));
};

关于javascript - 声明 HTTP 和 HTTPS REST 服务器时保持 DRY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33686410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com