javascript - 将参数函数Node.js从一个js传递到另一个

标签 javascript json node.js function express

我正在尝试将函数响应从一个 Node.js 脚本传递到另一个

这是我正在尝试的。 索引.js

var express = require('express');

require('./meter');

var app = express();

app.get('/',function(req,res){
    return;                    //hi function from another file
})

app.listen(3000);
console.log('listening on 3000 port')

meter.js

module.exports = {

    hi: function(req, res) {
        return res.status(200).json({"message" : "Hello V1"});
    }
};

只需要函数就可以完成这项工作吗?

提前致谢。

最佳答案

当你使用require时,你应该把它赋值给一个变量,然后你就可以在你的代码中使用它了:

var express = require('express');
var meter = require('./meter');  // meter will be an object you can use later on
var app = express();

app.get('/',meter.hi); // you actually don't need another annonimous function, can use hi directly as the callback

app.listen(3000);
console.log('listening on 3000 port')

关于javascript - 将参数函数Node.js从一个js传递到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40560106/

相关文章:

javascript - 隐藏jquery和javascript

javascript - 如何从对象数组中获取从未出现在特定属性中的值

javascript - jQuery 解析 Json 多维数组

javascript - Express Router 对一个 url 的 post 方法返回 404

node.js - 通过将 http 服务器传送到 http 请求的简单 node.js 代理

node.js - 根据请求参数 express 有条件地使用中间件

javascript - 向符号添加类使符号出现在下一行

javascript - 交错图像和复选框的问题

php - 通过 cURL 访问 import.io API 时无法获取 JSON 结果中的实际 html

c# - 如何从json中删除多余的封装大括号?