最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

我怎样才能使从外部请求我的本地服务器?

IT培训 admin 3浏览 0评论

我怎样才能使从外部请求我的本地服务器?

我在我的上的端口本地主机上运行一个简单的服务器的NodeJS:3434

const cors = require('cors');
const app = require('express')();
const bodyParser = require('body-parser')

app.use(bodyParser.json())
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(cors());

    app.get('/ping/:anystring', (req, res) => {
            console.log(req.params['anystring']);
            res.send({
                anystring: req.params['anystring']
            })
        });

    app.listen(3434);

我想从我的网站上执行一些Ajax调用。我试图配置路由器端口转发,如下所示:

- name service: mylocalserver   
- porta ragnge: 3434    
- local ip: 192.168.1.19
- local port: 3434
- protocol: BOTH

但是当我做

fetch(publicIP:3434/ping/hello).then(res => {
  console.log(res);
})

我收到错误404

可能有人帮助我,告诉我我做错了什么?

回答如下:

除非你创建一个隧道,你不能访问你的局域网之外的本地主机服务器。我用ngrok。

没有为ngrok的npm package,但我无法得到一个工作,所以我只是手动启动从终端服务器时,我需要测试的API。

你还需要http。

添加到您的app.js:

const http = require('http');

const newPort = //your port here (needs to be a different port than the port your app is currently using)

const server = http.createServer(function(req, res) {

  console.log(req); //code to handle requests to newPort
  res.end('Hello World);
});

app.listen(newPort, function() {
    console.log(`ngrok listening on ${newPort}`);
});

现在,在终端,安装ngrok后,使用此ngrok http newPort其中新港=你的端口

你可以去localhost:4040(这可能会改变取决于你的系统)查看发送到您的服务器的请求

发送一个请求到localhost,这样做:

- name service: mylocalserver //not sure
- porta ragnge: ???
- local ip: //ngrok gives you a url in terminal when you start the server (I'm not sure if you can reference an IP)
- local port: newPort
- protocol: http //(ngrok gives you a different url for http and https)

我怎样才能使从外部请求我的本地服务器?

我在我的上的端口本地主机上运行一个简单的服务器的NodeJS:3434

const cors = require('cors');
const app = require('express')();
const bodyParser = require('body-parser')

app.use(bodyParser.json())
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(cors());

    app.get('/ping/:anystring', (req, res) => {
            console.log(req.params['anystring']);
            res.send({
                anystring: req.params['anystring']
            })
        });

    app.listen(3434);

我想从我的网站上执行一些Ajax调用。我试图配置路由器端口转发,如下所示:

- name service: mylocalserver   
- porta ragnge: 3434    
- local ip: 192.168.1.19
- local port: 3434
- protocol: BOTH

但是当我做

fetch(publicIP:3434/ping/hello).then(res => {
  console.log(res);
})

我收到错误404

可能有人帮助我,告诉我我做错了什么?

回答如下:

除非你创建一个隧道,你不能访问你的局域网之外的本地主机服务器。我用ngrok。

没有为ngrok的npm package,但我无法得到一个工作,所以我只是手动启动从终端服务器时,我需要测试的API。

你还需要http。

添加到您的app.js:

const http = require('http');

const newPort = //your port here (needs to be a different port than the port your app is currently using)

const server = http.createServer(function(req, res) {

  console.log(req); //code to handle requests to newPort
  res.end('Hello World);
});

app.listen(newPort, function() {
    console.log(`ngrok listening on ${newPort}`);
});

现在,在终端,安装ngrok后,使用此ngrok http newPort其中新港=你的端口

你可以去localhost:4040(这可能会改变取决于你的系统)查看发送到您的服务器的请求

发送一个请求到localhost,这样做:

- name service: mylocalserver //not sure
- porta ragnge: ???
- local ip: //ngrok gives you a url in terminal when you start the server (I'm not sure if you can reference an IP)
- local port: newPort
- protocol: http //(ngrok gives you a different url for http and https)
发布评论

评论列表 (0)

  1. 暂无评论