在 node.js 服务器中引用我的 api 模块时,我遇到了让我的 node.js 服务器运行的问题。
public static async void Start()
{
var data = 9;
var createHttpServer = Edge.Func(File.ReadAllText("../../../../server/server.js"));
await createHttpServer(new
{
port = 3333,
});
}
static void Main(string[] args)
{
Task.Run((Action)Start).Wait();
Application.Run(new Form1());
}
服务器.js
返回函数(选项,cb){
'use strict';
var api = require('routes/api');
var express = require('express'); // web framework
var cors = require('cors'); // middleware for express
var bodyParser = require('body-parser'); // parsing module
var path = require('path');
var app = express();
app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json());
//app.get('/api/getDeviceCatalog', api.getDeviceCatalog);
//app.get('/api/getDeviceDetails', api.getDeviceDetails);
//app.get('/api/getDeviceImages', api.getDeviceImages);// parse application/json
// IMPORTANT!: This rule has to be last rule for routing as it has very common definition
app.all('/*', function (request, response) {
var fileName, serverPath;
console.log('Send: ' + __dirname + request.url);
serverPath = path.resolve(__dirname + '../../../../../../server/');
fileName = serverPath + request.url;
response.sendFile(fileName);
});
// start server listening on defined port
app.listen(options.port);
console.log('The NodeJS server is ready.');
};
如果删除此行,我可以启动服务器并正常运行
var api = require('routes/api');
我收到一个错误
$exception {"Error: Cannot find module 'routes/api'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at eval (eval at <anonymous
(C:\\TechSpikeUDC\\TechSpikeUDC\\TechSpikeUDC\\bin\\x86\\Debug\\edge\\double_edge.js:34:28), <anonymous>:37:15)"} System.Exception