gpt4 book ai didi

javascript - 服务器没有收到 Angular http 请求

转载 作者:搜寻专家 更新时间:2023-11-01 00:36:47 24 4
gpt4 key购买 nike

<分区>

我会将数据发送到我的数据库。

所以我在调用我的服务的 component.ts 中创建了我的函数

ajoutText(newtext: String) {
this.dataService.sendtext(newtext);
}

在 HTML 端

<form class="form">
<mat-form-field class="textinput">
<input #newtext matInput placeholder="Ajoutez votre texte" value="">
</mat-form-field>
<button type="button" mat-raised-button color="primary" (click)="ajoutText(newtext.value)">Ajouter</button>

</form>

然后我的服务向服务器发送数据

export class AjouttextService {
url = "http://127.0.0.1:8080/v1";
constructor(private http: HttpClient) { }

sendtext(text): Observable<string> {
console.log(text);
return this.http.post<string>(this.url, text);
}
}

我的 console.log(text) 可以很好地打印我在输入字段中输入的文本。

在我的服务器端我使用 Express

var http = require("http");
var admin = require('firebase-admin');
var firebase = require("firebase");
var express = require("express");
var app = express();
var routerProj = require("./routes/routes");
var bodyParser = require("body-parser");
var port = process.env.app_port || 8080; // set our port
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var server = app.listen(port);

app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT ,DELETE');

res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,*");
next();
});
var config = {
apiKey: "XXXXXXXXXXX",
authDomain: "datatable-18f93.firebaseapp.com",
databaseURL: "https://datatable-18f93.firebaseio.com",
projectId: "datatable-18f93",
storageBucket: "datatable-18f93.appspot.com",
messagingSenderId: "282475200118"
};
firebase.initializeApp(config);


var serviceAccount = require("./ServiceAcountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://datatable-18f93.firebaseio.com"
});
app.use("/v1", routerProj);
//Create HTTP server and listen on port 8000 for requests

// Print URL for accessing server
console.log("Server running at http://127.0.0.1:8080/");

然后在我的路由器中,我将数据插入到我的 Firebase 数据库中

router.use(function (req, res, next) {
// do logging
console.log("HI");
next(); //
});
...
router
.route("/")
.post(function (req, res, err) {
console.log("test")
// Get a database reference to our posts
var db = admin.database();
var ref = db.ref("/");

// Attach an asynchronous callback to read the data at our posts reference
ref.push(
{
"text": req.body
}

);

});

问题是我的服务器没有检测到客户端发送的请求。路由第一行的 console.log("test") 不会在我的控制台上打印“test”,console.log('Hi') ..

服务器正在运行,或者是客户端。我不知道为什么路由没有被使用。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com