我正在構建 NodeJS 代碼來偵聽來自特定埠的請求并回傳回應,這里是主要代碼:
module.exports = function (port) {
var fs = require("fs");
var path = require("path");
var express = require('express');
var vhost = require('vhost');
var https = require('https');
var http = require('http');
var bodyParser = require("body-parser");
var normalizedPath = require("path").join(__dirname, "../BlazeData/ssl/");
var options = {
key: fs.readFileSync(normalizedPath 'spring14.key'),
cert: fs.readFileSync(normalizedPath 'spring14.cert'),
};
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var normalizedPath = path.join(__dirname, "../WebServices");
fs.readdirSync(normalizedPath).forEach(function(file) {
if (file.indexOf('.js') != -1) {
var url = file.substring(0, file.length - 3);
app.use(vhost(url, require(normalizedPath "/" file).app));
console.log( 'Registered Service -> %s:%d', url, port );
}
});
if (port == 80) {
var server = http.createServer(app).listen(port, function(){
console.log("Create HTTP WebServices");
console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});
}
if (port == 443) {
var server = https.createServer(options, app).listen(port, function(){
console.log("Create HTTPS WebServices");
console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});
}
}
我有另一個 JS 檔案用于運行上面的腳本,我用來
var https1 = require('./clientAuthServer')從上面啟動代碼,其中 clientAuthServer.js 是主代碼的檔案名,但是它只是跳過了該檔案中的所有內容。
我將如何module.exports = function (port)從單獨的檔案中呼叫并為函式正在使用的引數“埠”賦值?
uj5u.com熱心網友回復:
當您需要模塊時,它會回傳一個函式(模塊匯出的函式)。該函式被分配給變數https1,因此您只需要呼叫該函式,因為現在它只是被存盤。
最簡單的方法是讓您的 require 陳述句看起來像這樣:
const https1 = require("./clientAuthServer")(parameter);
parameter你想傳遞給函式的任何值在哪里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/503753.html
標籤:javascript 节点.js ssl 听众
上一篇:TLS1.3中的延遲證書
下一篇:如何設定無法解密流量的反向代理?
