测试利器Mocha 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

项目重构

我们的app.js需要最外部暴露两个方法。所以要对我们右边的项目进行修改。

需要修改的代码:

var server = http.createServer(onRequest);
server.listen(PORT);

重构为

var server = http.createServer(onRequest);
var boot = function () {
    server.listen(PORT, function () {
        console.info('Express server listening on port ' + PORT);
    });
};
var shutdown = function () {
    server.close();
};
if (require.main === module) {
    boot();
} else {
    console.info('Running app as a module');
    exports.boot = boot;
    exports.shutdown = shutdown;
}

重构描述:现在我们把启动服务和关闭服务分别进行了封装并且对外进行了暴露。

按照上述中的描述改写 app.js 中的代码。