Export variables from the Node JS server script

0

I'm trying to export a variable from my script (Server.js) to another script (articleHandler.js) using the following code line in the server script

module.exports = myVariable;

Which has not given me any success, since when executing the script articleHandler.js it returns the following error

  

Creating routers ...

     

Routers created.

     

Running @ localhost: 3000

     

[Function: queryExport]

     

events.js: 183

     

throw er; // Unhandled 'error' event

     

^

     

Error: listen EADDRINUSE ::: 3000

     

at Object._errnoException (util.js: 992: 11)

     

at _exceptionWithHostPort (util.js: 1014: 20)

     

at Server.setupListenHandle [as _listen2] (net.js: 1355: 14)

     

at listenInCluster (net.js: 1396: 12)

     

at Server.listen (net.js: 1480: 7)

     

at Function.listen

     

(/ Users / default / node_modules / express / lib / application.js: 618: 24)

     

at Object. (/Users/default/project/DemoPage/server.js:46:5)

     

at Module._compile (module.js: 652: 30)

     

at Object.Module._extensions..js (module.js: 663: 10)

     

at Module.load (module.js: 565: 32)

This is my script Server.js

const express = require("express");
const app = express();
const path = require("path");
const bodyParser = require("body-parser");
const port = parseInt(process.env.PORT, 10) || 3000;
var queryResponse;

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(path.join(__dirname, 'www')));
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());

console.log("Creating routers...");
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});


app.get('/new-article', function (req, res) {
res.sendFile(path.join(__dirname + '/www/admin-dashboard/new- 
article.html'));
});
console.log("Routers created.");


app.post("/new-article", function (req, res) {
queryResponse = req.body.articleName;
console.log("New article: " + queryResponse);

res.send({
query: queryResponse
});
});

//Esto no funciona
var queryExport = function(){
return queryResponse;
}
module.exports = queryExport;

//Esto tampoco
//module.exports.queryResponse = queryResponse;

app.listen(port);
console.log("Running @ localhost:3000");

And this is my articleHandler.js script in which I want to import the queryResponse tag from > Server.js

let queryResponse = require("./server.js");
console.log(queryResponse);

Any idea why this error is due?

    
asked by matias 14.10.2018 в 07:26
source

1 answer

2

You almost have it. You only need one thing: since you're exporting a feature, you have to call it :

let queryResponse = ( require("./server.js") )( );

console.log(queryResponse);

You could also do, in your server.js :

var queryResponse;
...
module.exports = queryResponse;

With what you could import it like this:

let externalQueryResponse = require( './server.js' );

Or you could even, in your server.js :

var queryResponse;
...
module.exports = {
  queryResponse: queryResponse
}

And then the you import like this:

const ServerJs = require( './server.js' );

console.log( ServerJs.queryResponse );

You could also use import , but we are already extending too: -)

    
answered by 14.10.2018 / 08:11
source