在module文件夹中我用两种方式进行暴露自定义模块 temp.js
//采用module.exports
var obj = {
formateApi(api) {
return "www.baidu.com" + api
}
}
module.exports = obj
tools.js
//采用exports
exports.get = function () {
return "获取数据"
}
exports.post = function () {
return "传输数据"
}
在app.js里引入自定义的两个模块
var http = require('http');
// 引入模块
const tools = require('./module/tools.js')
const temp = require('./module/temp.js')
http.createServer((req, res) => {
res.writeHead(200, {
'Content-Type': "text/html;charset='utf-8'"
});
res.write("<head> <meta charset='UTF-8'> </head>")
console.log(temp.formateApi("/id=13"))
console.log(tools.get())
res.end();
}).listen(8081);
npm init --yes
可以生成package.json文件,生成版本信息和配置入口文件等
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有