最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using express.js to execute a function after receiving an http request? - Stack Overflow

programmeradmin2浏览0评论

So I have an angular controller that successfully makes an http request after a button is pressed. I have a node.js server running which gets the request and then sends back YOU HAVE SUCCEEDED! HTTP REQUEST RECEIVED!. I want to execute a function when the request is received, but the function lives in a file inside a folder which is in ./Source/Server/test.js.

How do I tell my server.js file to send the mand to run the function???

server.js:

var express = require('express');
var app = express();
var fs = require('fs');
app.use(express.static(__dirname + '/Source/Client/Templates'));
app.use(express.static(__dirname + '/'));
app.get('/', function (req, res) {
    res.sendFile('/Source/Client/Templates/Home.html', {root: __dirname });
});
app.get('/ThisIsATest', function (req, res) {
  res.send('YOU HAVE SUCCEEDED! HTTP REQUEST RECEIVED!')
})
app.listen(3000);
console.log("running at port 3000");

test.js:

var run = require('./quickstart.js');
run.runQuickstart();

Controller:

angular.module('DaycareApp').controller('WaitingListController', ['$scope', '$http', function($scope, $http){
    $scope.test = function() {
        $http.get('/ThisIsATest')
        .then(function(response) {
            alert(response.data);
        });
    };
}]);

So I have an angular controller that successfully makes an http request after a button is pressed. I have a node.js server running which gets the request and then sends back YOU HAVE SUCCEEDED! HTTP REQUEST RECEIVED!. I want to execute a function when the request is received, but the function lives in a file inside a folder which is in ./Source/Server/test.js.

How do I tell my server.js file to send the mand to run the function???

server.js:

var express = require('express');
var app = express();
var fs = require('fs');
app.use(express.static(__dirname + '/Source/Client/Templates'));
app.use(express.static(__dirname + '/'));
app.get('/', function (req, res) {
    res.sendFile('/Source/Client/Templates/Home.html', {root: __dirname });
});
app.get('/ThisIsATest', function (req, res) {
  res.send('YOU HAVE SUCCEEDED! HTTP REQUEST RECEIVED!')
})
app.listen(3000);
console.log("running at port 3000");

test.js:

var run = require('./quickstart.js');
run.runQuickstart();

Controller:

angular.module('DaycareApp').controller('WaitingListController', ['$scope', '$http', function($scope, $http){
    $scope.test = function() {
        $http.get('/ThisIsATest')
        .then(function(response) {
            alert(response.data);
        });
    };
}]);
Share Improve this question asked Nov 7, 2016 at 18:20 RyanRyan 2624 silver badges19 bronze badges 2
  • It sounds like you want to move the contents of test.js into the handler, right before you're calling res.send(...). – user5734311 Commented Nov 7, 2016 at 18:27
  • @ChrisG So something along the lines of: var runQuickstart = require('./Source/Server/quickstart.js'); app.get('/ThisIsATest', function (req, res, runQuickstart) {runQuickstart.runQuickstart();}) should do the trick? It says runQuickstart() is not a function but maybe I'm passing the variable in wrong or something.... – Ryan Commented Nov 7, 2016 at 18:37
Add a ment  | 

4 Answers 4

Reset to default 2

In test.js you can export your module like this

var run = require('./quickstart.js');

function mySpecialFunction() {
    return run.runQuickstart();
}

module.exports = {
    mySpecialFunction: mySpecialFunction
};

Now in your server.js or wherever your controller is you can call the function by inluding test.js in your module using require.

var test = require('./test.js');

app.get('/ThisIsATest', function (req, res) {
  res.send(test.mySpecialFunction());
});

You can require test.js as a file after exposing the necessary functions, like this:

// test.js
// ========
var run = require('./quickstart.js');

module.exports = {
  run: function () {
    run.runQuickstart();
  }
};

In server.js, require it:

var test = require('./Source/Server/test');

and use it with: test.run().

Could you do something like:

server.js

var express = require('express');
var app = express();
var fs = require('fs');
var run = require('./path/to/quickstart.js');

// ...

app.get('/ThisIsATest', function (req, res) {
  run.runQuickstart();
  res.send('YOU HAVE SUCCEEDED! HTTP REQUEST RECEIVED!')
})

// ...

And then in test.js:

var run = require('./quickstart.js');

exports.runQuickstart = function() {
  run.runQuickstart();
};

You can require the file

var outsideFunction  = require(__dirname + '/Source/Server/test.js');

then add it to the route.

app.get('/ThisIsATest', function (req, res) {
      outsideFunction(req, res);
      res.send('YOU HAVE SUCCEEDED! HTTP REQUEST RECEIVED!')
})
发布评论

评论列表(0)

  1. 暂无评论