[英]What's the domain name of my node.js server?
我正在嘗試從服務器獲取數據到我的顫振應用程序。 這是我在 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);
這是我的顫振代碼,我試圖從服務器獲取一個字符串,但是當我在顫振控制台中打印時該字符串返回 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));
}
在這里,我正在嘗試打印數據
Future<void> getdata() async {
var result= await http_get('get');
print(result.data);
}
可以看到,域沒有問題,嘗試去掉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));
}
這里您的查詢參數?data=$dataStr
不可用,因為您只設置了路由而不是后端中的查詢。
您可以將您的獲取請求發送到/get
route
當你說服務器是在你的本地機器上,你的顫振也在運行
如果是,那么您的域是 localhost:3000
else 域名是托管和運行 Node 代碼的服務器的 IP,(如果您使用 IP 屏蔽了 DNS,則可以使用類似http://myserver.com 的內容,myserver 是您假設屏蔽的域名)
如果您的 flutter 應用程序在移動設備上運行,而 nodeJs 運行在本地機器上,則您需要與您的機器在同一網絡上才能訪問 localhost:3000
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.