【问题标题】:Testing Node Express API routes via remote http-client通过远程 http-client 测试 Node Express API 路由
【发布时间】:2018-05-16 11:06:24
【问题描述】:

我有以下情况:在我的本地机器上运行 express 的节点和我网络的私有地址空间内的远程 linux-box。

这是使用 nodejs & express 的紧凑 index.js 示例:

const express = require('express');
const app = express();

app.get('/', (req,res) => {
    res.send('RESTful Demo..');
});

app.get('/api/read', (req,res) => {
    console.log('test 123..');
    res.send([1, 2, 3]);
});

// Array to simulate some db-entries
const jsObject = [
    { pk: 1, value: 'data1'},
    { pk: 2, value: 'data2'},
    { pk: 3, value: 'data3'}
];

app.get('/api/read/:pk', (req,res) => {
    var pk = req.params.pk;
    console.log(pk);
    let myMatchedObject = jsObject.find(c => c.pk === parseInt(req.params.pk));
    if (!myMatchedObject) res.status(404).send('The given Primaray Key was not found..'); // 404
    res.send(myMatchedObject);
});

// PORT envar
const port = process.env.PORT || 3000;
// Listening-process
app.listen(port, () => console.log(`Listening on Port ${port}..`));

我已通过本地浏览器和远程浏览器成功尝试过,但我已尝试从我网络的私人地址空间内的远程 Linux-Box 进行 curl,但我无法调用 API。

我尝试过(启用代理隧道)curl -o test.txt -p http://192.168.1.152:3000/api/read 没有成功..

然后我用 curl https://jsonplaceholder.typicode.com/users 对 jsonplaceholder API 进行了一些成功的测试

考虑到这一点,我克隆了存储库并在我的本地机器上尝试了它 - 再次没有来自 curl 的响应。

我缺少什么基本思想?

【问题讨论】:

  • 问题已解决 - 网络限制..

标签: node.js express curl


【解决方案1】:

由于我们的网络限制,我无法在我们的私人地址空间内蜷缩..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-26
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多