简体   繁体   English

我的 node.js 服务器的域名是什么?

[英]What's the domain name of my node.js server?

I am trying to fetch data from the server to my flutter app.我正在尝试从服务器获取数据到我的颤振应用程序。 This is my server side code in Node.js:这是我在 Node.js 中的服务器端代码:

const express=require('express');
const app=express();
app.use(express.json());
const getres='himu you got the request';
app.post('/post',async(req,res,next)=>{
const name= req.body.name;
console.log(name);
next();
}
);

app.get('/get',async(req,res,next)=>{
res.end('hello world');
res.json('hey from nodejs');
next();
}
);

app.listen(3000);

This is my flutter code where I am trying to fetch a string from the server but the string is returning null when I am printing in the flutter console.I am confused what should be my DOMAIN .这是我的颤振代码,我试图从服务器获取一个字符串,但是当我在颤振控制台中打印时该字符串返回 null。我很困惑我的DOMAIN应该是什么。

const PROTOCOL = "http";
const DOMAIN = "localhost:3000";

Future<RequestResult> http_get(String route, [dynamic data]) async
{
  var dataStr = jsonEncode(data);
  var url = "$PROTOCOL://$DOMAIN/$route?data=$dataStr";
  var result = await http.get(url);
  return RequestResult(true, jsonDecode(result.body));
}

Here I am trying to print the data在这里,我正在尝试打印数据

 Future<void> getdata() async {
   var result= await http_get('get');
   print(result.data);
  }

As seen, there is no problem with domain, try removing the query parameter in the url variable可以看到,域没有问题,尝试去掉url变量中的查询参数

const PROTOCOL = "http";
const DOMAIN = "localhost:3000";

Future<RequestResult> http_get(String route, [dynamic data]) async
{
  var dataStr = jsonEncode(data);
  var url = "$PROTOCOL://$DOMAIN/$route?data=$dataStr";
  var result = await http.get(url);
  return RequestResult(true, jsonDecode(result.body));
}

Here your query parameter ?data=$dataStr is not availabe as you have only set the routes not the queries in the backend.这里您的查询参数?data=$dataStr不可用,因为您只设置了路由而不是后端中的查询。
You can send your get request to /get route您可以将您的获取请求发送到/get route

When you say the server is it on your local machine on which your flutter is also running当你说服务器是在你的本地机器上,你的颤振也在运行

if yes, then your domain is localhost:3000如果是,那么您的域是 localhost:3000

else domain name is IP of the server where your Node code is hosted and running, (if you have DNS masked with IP, you can you use something like http://myserver.com , myserver is the domain name you masked hypothetically) else 域名是托管和运行 Node 代码的服务器的 IP,(如果您使用 IP 屏蔽了 DNS,则可以使用类似http://myserver.com 的内容,myserver 是您假设屏蔽的域名)

if your flutter app is running on mobile device and nodeJs on the local machine, you will need to be on the same network with your machine to access localhost:3000如果您的 flutter 应用程序在移动设备上运行,而 nodeJs 运行在本地机器上,则您需要与您的机器在同一网络上才能访问 localhost:3000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM