Express 如何通过提取Nativescript发出post请求?

Express 如何通过提取Nativescript发出post请求?,express,post,request,fetch,nativescript,Express,Post,Request,Fetch,Nativescript,我有一个server.js文件。使用post请求(/login)。看起来是这样的: require('dotenv').config(); const express = require('express'); const mysql = require('mysql') const app = express(); const PORT = process.env.PORT || 3000 app.listen(PORT, console.log(`Server started on port

我有一个server.js文件。使用post请求(/login)。看起来是这样的:

require('dotenv').config();
const express = require('express');
const mysql = require('mysql')
const app = express();
const PORT = process.env.PORT || 3000
app.listen(PORT, console.log(`Server started on port ${PORT}`))


app.post('/login', (req, res) => {
    console.log("login")
    console.log(req);
})
export function onLoginButtonTap() {
    console.log("login button tapped")
    const frame = Frame.getFrameById("mainFrame");

    // TODO: user authentication
    var userEmail = `${bindingContext.get('email')}`;
    var userPassword = `${bindingContext.get('password')}`;

    // make sure fields are filled out
    if (userPassword === "" || userEmail === "") {
        alert({
            title: "Login Error",
            message: "One or more fields is empty. Please fill in every field.",
            okButtonText: "OK",
        })
    } else {
        // TODO: post request (send user input to backend for verification)
        console.log("here1")
        const data = {userEmail, userPassword};
        const options = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }
        fetch("http://localhost:3000/login", options);
    
        // if user is in database change page to home
        frame.navigate("pages/home/home-page");
    }

    // navigates to home page without requiring login info (for testing)
    frame.navigate("pages/home/home-page");
}
我在NativeScript中还有一个函数,当按下一个按钮时,它应该发出post请求(取数在哪里)。看起来是这样的:

require('dotenv').config();
const express = require('express');
const mysql = require('mysql')
const app = express();
const PORT = process.env.PORT || 3000
app.listen(PORT, console.log(`Server started on port ${PORT}`))


app.post('/login', (req, res) => {
    console.log("login")
    console.log(req);
})
export function onLoginButtonTap() {
    console.log("login button tapped")
    const frame = Frame.getFrameById("mainFrame");

    // TODO: user authentication
    var userEmail = `${bindingContext.get('email')}`;
    var userPassword = `${bindingContext.get('password')}`;

    // make sure fields are filled out
    if (userPassword === "" || userEmail === "") {
        alert({
            title: "Login Error",
            message: "One or more fields is empty. Please fill in every field.",
            okButtonText: "OK",
        })
    } else {
        // TODO: post request (send user input to backend for verification)
        console.log("here1")
        const data = {userEmail, userPassword};
        const options = {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }
        fetch("http://localhost:3000/login", options);
    
        // if user is in database change page to home
        frame.navigate("pages/home/home-page");
    }

    // navigates to home page without requiring login info (for testing)
    frame.navigate("pages/home/home-page");
}

post请求不起作用。我希望服务器文件中的console.logs打印请求。我想问题在于我是如何写这个帖子的?当前没有发生任何事情。

获取并返回承诺,您需要解决它以实际发出
POST
请求。请尝试以下代码块

fetch('http://localhost:3000/login', options)
          .then((response) => response.json())
          .then((result) => {
            console.log('Success:', result);
          })
          .catch((error) => {
            console.error('Error:', error);
          });

顺便说一句,前端和后端在完全不同的目录中。我尝试了一下,得到了这个输出“Error:TypeError:networkrequest failed”。我刚刚意识到。。。是“本地主机”。。。我一直在我的手机上做测试。。。哇!不过,它应该可以与仿真器一起工作。我会试一试。也许先试着在同一台机器上运行它,把问题分成更小的单元,然后一个接一个地解决它们,所以我所做的就是在Heroku上用一个worker dyno(不是web)启动express server,我所做的是将fetch改为“fetch('https:/myprojectname/herokuapp.com/login'),并打印“Error:”+错误部分。错误是“SyntaxError:Unexpected token<在JSON中的位置0”。我假设它没有正确解析JSON?是的,这是无效JSON的常见错误。