我目前正在做一个项目,需要我使用flutter前端和NodeJS后端构建一个移动的应用程序。我从OVHcloud购买了一个运行Ubuntu 20.04的VPS,并遵循多个教程为我创建了一个服务器。我写了一些非常基本的前端和后端代码,只是为了测试连接,并确保数据被发送到服务器,响应被发回给我。我还禁用了OVHcloud VPS防火墙以防止任何问题,尽管我计划很快启用它,对于任何想知道的人来说,我添加的防火墙规则基本上与教程here相同,除了不同的SSH端口。
以下是我的Flutter前端代码:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:nagahni/client.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyProject',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'MyProject'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? _message;
// This function will send the message to our backend.
void sendMessage(msg) {
WebSocketChannel? channel;
try {
channel = IOWebSocketChannel.connect('ws://I put my IP here:I put my port here');
print('connected');
} catch (e) {
if (kDebugMode) {
print(
"Error: $e");
}
}
channel?.sink.add(msg);
channel?.stream.listen((event) {
if (event != null) {
if (kDebugMode) {
print(event);
}
}
channel!.sink.close();
});
if (kDebugMode) {
print(msg);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: [
Center(
child: TextField(
onChanged: (e) => _message = e,
),
),
Center(
child: TextButton(
child: const Text("Send String"),
onPressed: () {
// Check if the message isn't empty.
if (_message!.isNotEmpty) {
sendMessage(_message);
}
},
),
),
const SizedBox(height: 12),
TextButton(
onPressed: () {
sendObject();
},
child: const Text('Send Object'),
),
],
),
),
);
}
}
以下是我用TypeScript编写的后端NodeJS代码:
import express from 'express';
import { Server } from 'ws';
const PORT = process.env.MAIN_PORT; // This is basically the SSH port + 1 ex. if 22, then this would be 23
require('dotenv').config()
const server = express()
.use((req, res) => res.send("Hi there"))
.listen(PORT, () => console.log(`Listening on Port ${PORT}`))
const wss = new Server({ server });
wss.on('connection', function(ws, req) {
ws.on('message', message => {
var data = message.toString();
if(data === "Hello") {
console.log(data);
ws.send("Hi from TypeScript NodeJS");
} else {
console.log(data);
ws.send("Say hello to me!");
}
})
})
现在我的问题是,从前端来看,终端显示connected
沿着我们发送的字符串消息。在后端,所有的都是说它正在监听端口23,例如,但它不打印任何数据或响应或做任何事情。
我注意到的是,当使用tsc --w
时,由于某种原因,我从express模块得到147个错误,但随后它正常编译文件,并说它正在侦听端口。更奇怪的是,如果我在我的PC上运行相同的代码,我不会得到一个错误,一切都将正常编译。但它同样不会在我的终端显示任何数据,因为它也不应该返回响应。
express模块的错误给任何感兴趣的人:
[5:51:56 PM] Starting compilation in watch mode...
node_modules/@types/express-serve-static-core/index.d.ts:104:68 - error TS1110: Type expected.
104 type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S;
~~~
node_modules/@types/express-serve-static-core/index.d.ts:104:77 - error TS1005: '}' expected.
104 type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S;
~
node_modules/@types/express-serve-static-core/index.d.ts:104:78 - error TS1128: Declaration or statement expected.
104 type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S;
~
node_modules/@types/express-serve-static-core/index.d.ts:104:80 - error TS1005: ';' expected.
104 type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S;
~
node_modules/@types/express-serve-static-core/index.d.ts:106:33 - error TS1005: ';' expected.
106 RemoveTail<RemoveTail<S, `/${string}`>, `-${string}`>,
~
node_modules/@types/express-serve-static-core/index.d.ts:106:48 - error TS1005: ';' expected.
106 RemoveTail<RemoveTail<S, `/${string}`>, `-${string}`>,
~
node_modules/@types/express-serve-static-core/index.d.ts:107:8 - error TS1005: ';' expected.
107 `.${string}`
~
node_modules/@types/express-serve-static-core/index.d.ts:113:22 - error TS1005: ';' expected.
113 : Route extends `${string}(${string}`
~
node_modules/@types/express-serve-static-core/index.d.ts:113:23 - error TS1005: ';' expected.
113 : Route extends `${string}(${string}`
~
node_modules/@types/express-serve-static-core/index.d.ts:113:33 - error TS1005: ')' expected.
113 : Route extends `${string}(${string}`
~
node_modules/@types/express-serve-static-core/index.d.ts:115:26 - error TS1005: ';' expected.
115 : Route extends `${string}:${infer Rest}`
~
node_modules/@types/express-serve-static-core/index.d.ts:115:27 - error TS1005: ';' expected.
115 : Route extends `${string}:${infer Rest}`
~
node_modules/@types/express-serve-static-core/index.d.ts:115:35 - error TS1128: Declaration or statement expected.
115 : Route extends `${string}:${infer Rest}`
~
node_modules/@types/express-serve-static-core/index.d.ts:115:37 - error TS1005: ';' expected.
115 : Route extends `${string}:${infer Rest}`
~
node_modules/@types/express-serve-static-core/index.d.ts:115:44 - error TS1005: ';' expected.
115 : Route extends `${string}:${infer Rest}`
~~~~
node_modules/@types/express-serve-static-core/index.d.ts:119:52 - error TS1005: ';' expected.
119 : GetRouteParameter<Rest> extends `${infer ParamName}?`
~
node_modules/@types/express-serve-static-core/index.d.ts:119:53 - error TS1005: ';' expected.
119 : GetRouteParameter<Rest> extends `${infer ParamName}?`
~
node_modules/@types/express-serve-static-core/index.d.ts:119:60 - error TS1005: ';' expected.
119 : GetRouteParameter<Rest> extends `${infer ParamName}?`
~~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:119:70 - error TS1128: Declaration or statement expected.
119 : GetRouteParameter<Rest> extends `${infer ParamName}?`
~
node_modules/@types/express-serve-static-core/index.d.ts:123:28 - error TS1005: ';' expected.
123 (Rest extends `${GetRouteParameter<Rest>}${infer Next}`
~
node_modules/@types/express-serve-static-core/index.d.ts:123:29 - error TS1005: ';' expected.
123 (Rest extends `${GetRouteParameter<Rest>}${infer Next}`
~
node_modules/@types/express-serve-static-core/index.d.ts:123:53 - error TS1005: '(' expected.
123 (Rest extends `${GetRouteParameter<Rest>}${infer Next}`
~
node_modules/@types/express-serve-static-core/index.d.ts:123:55 - error TS1005: ';' expected.
123 (Rest extends `${GetRouteParameter<Rest>}${infer Next}`
~
node_modules/@types/express-serve-static-core/index.d.ts:123:62 - error TS1005: ';' expected.
123 (Rest extends `${GetRouteParameter<Rest>}${infer Next}`
~~~~
node_modules/@types/express-serve-static-core/index.d.ts:239:41 - error TS1005: ';' expected.
239 * Map the given param placeholder `name`(s) to the given callback(s).
~~~~
node_modules/@types/express-serve-static-core/index.d.ts:248:31 - error TS1005: ';' expected.
248 * of the user. Once the `next()` function is invoked, just like middleware
~~~~
node_modules/@types/express-serve-static-core/index.d.ts:274:62 - error TS1005: ';' expected.
274 * Special-cased "all" method, applying the given route `path`,
~~~~
node_modules/@types/express-serve-static-core/index.d.ts:372:49 - error TS1005: ';' expected.
372 * @param P For most requests, this should be `ParamsDictionary`, but if you're
~~~~~~~~~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:373:59 - error TS1005: ';' expected.
373 * using this in a route handler for a route that uses a `RegExp` or a wildcard
~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:374:5 - error TS1005: ';' expected.
374 * `string` path (e.g. `'/user/*'`), then `req.params` will be an array, in
~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:374:25 - error TS1005: ';' expected.
374 * `string` path (e.g. `'/user/*'`), then `req.params` will be an array, in
~~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:374:44 - error TS1005: ';' expected.
374 * `string` path (e.g. `'/user/*'`), then `req.params` will be an array, in
~~~
node_modules/@types/express-serve-static-core/index.d.ts:375:31 - error TS1005: ';' expected.
375 * which case you should use `ParamsArray` instead.
~~~~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:380:84 - error TS1005: ';' expected.
380 * app.get('/user/:id', (req, res) => res.send(req.params.id)); // implicitly `ParamsDictionary`
~~~~~~~~~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:395:13 - error TS1005: ';' expected.
395 * The `Referrer` header field is special-cased,
~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:396:14 - error TS1005: ';' expected.
396 * both `Referrer` and `Referer` are interchangeable.
~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:396:29 - error TS1005: ';' expected.
396 * both `Referrer` and `Referer` are interchangeable.
~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:409:20 - error TS1005: ';' expected.
409 * Aliased as `req.header()`.
~~~
node_modules/@types/express-serve-static-core/index.d.ts:418:28 - error TS1005: ';' expected.
418 * Check if the given `type(s)` is acceptable, returning
~~~~
node_modules/@types/express-serve-static-core/index.d.ts:419:45 - error TS1005: ';' expected.
419 * the best match when true, otherwise `undefined`, in which
~~~~~~~~~
node_modules/@types/express-serve-static-core/index.d.ts:422:13 - error TS1005: ';' expected.
422 * The `type` value may be a single mime type string
~~~~
node_modules/@types/express-serve-static-core/index.d.ts:496:56 - error TS1005: ';' expected.
496 * Parse Range header field, capping to the given `size`.
~~~~
... wayy more errors but i cannot paste it all ...
[5:53:55 PM] Found 147 errors. Watching for file changes.
我为出现这些错误所采取的步骤如下:
- mkdir node_project
- cd node_project
- npm初始化-y
- npm i @types/express
- npm i express
- npm i @types/ws
- npm i ws
有人能帮我理解为什么数据无法发送/接收吗?
1条答案
按热度按时间9jyewag01#
你有没有试过检查https端口是否在vps上打开。尝试使用https检查API调用