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?