简体   繁体   中英

Creating a server and referencing API in node.js server (EdgeJS)

I am having issues with getting my node.js server running when referencing my api module in the node.js server.

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());
}

server.js

return function (options, 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.');
};

I can start the server and run it just fine if I remove this line

var api = require('routes/api');

I get an error

$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
var api = require('routes/api');

Your app can't find that file. Is your server.js at the same level as routes ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM