【发布时间】:2021-09-08 17:30:52
【问题描述】:
我真的需要这方面的帮助:
我在这里构建了我的第一个节点 js 服务器:
var express = require('express'),
app = express(),
https = require('https');
port = process.env.PORT || 443,
mongoose = require('mongoose'),
Task = require('./api/models/myCarModel'),
bodyParser = require('body-parser');
fs = require("fs");
const options = {
key: fs.readFileSync('./static/HttpsFile/privatekey.pem').toString(),
cert: fs.readFileSync('./static/HttpsFile/certificate.pem').toString(),
};
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Tododb');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var routes = require('./api/routes/myCarRoutes'); //importing route
routes(app); //register the route
https.createServer(options, app).listen(port);
console.log(options.key)
console.log('RESTful API server started on: ' + port);
这是我简单的 react-native 应用主页:
import React, { useState } from 'react';
import { useEffect } from 'react';
import { Button, StyleSheet, Text, TextInput, View } from 'react-native';
const Home = () => {
const [text, setText] = useState('');
useEffect(()=> {
debugger
fetch('https://192.168.1.8:443/cars', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}})
//axios.get("https://192.168.1.8:3000/cars")
.then((res:any) => {
debugger;
}, (err:any) => {
console.log(err)
debugger;
})
}, [])
return(...etc...)
现在: 当我第一次使用邮递员测试 API 时,我收到关于使用自签名证书(我通过 OpneSSL 使用)的错误。我恳请 Postman 接受这个想法,一切都很顺利。 这不是 react-native 的情况: 我正在使用 expo,在我的 android 手机上测试我的应用程序,当我调试 API 调用时,我在错误中收到以下错误:“TypeError: Network request failed”。
我不想通过让 Android 接受 HTTP 协议来解决问题。
大家好
【问题讨论】:
标签: node.js react-native types https openssl