我想知道使用 Next.js 页面连接到 WebSockets 服务器的最佳方法是什么?我希望用户可以通过一个连接浏览页面,并且当他关闭页面时,他也会关闭 WebSockets 连接。我尝试使用 React 的 Context API:
const WSContext = createContext(null);
const Wrapper = ({ children }) => {
const instance = WebSocket("ws://localhost:3000/ws");
return <WSContext.Provider value={instance}>{children}</WSContext.Provider>;
};
export const useWS = () => useContext(WSContext);
export default Wrapper;
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但在创建连接时却效果不佳。基本new WebSocket语法不起作用,所以我必须使用第三方库,就像react-use-websocket我不喜欢的那样。同样令人不安的是我无法关闭连接。Context 根本不知道页面何时关闭,而且库也没有提供用于关闭连接的钩子。
我想知道在 Next.js 中处理 WebSockets 连接的最佳方法是什么。
我想使用客户端的 React 和服务器端的 Express.js创建高级全栈项目。哪种文件夹结构实践是最好的?我想保持简单。该项目可能包含 TypeScript、E2E 和单元测试,它将与服务器端 API 通信并使用 Socket.io 进行实时集成。如果可能的话,我也想保留一个 node_modules 文件和一个 package.json。谢谢。
我正在尝试使用 TypeScript 在 Express 中创建一条路由,因此我像往常一样,将函数的参数设置createUser为req: Request、res:Response和next: NextFunction, from @types/express,显然。每次都有效,但这一次却显示错误:
No overload matches this call.
Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error.
Argument of type '(req: Request, res: Response, next: any) => Promise<any>' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.
Types of parameters 'req' and 'req' are incompatible.
Type 'Request<ParamsDictionary, any, any, ParsedQs>' is missing …Run Code Online (Sandbox Code Playgroud) 我正在从中获取数据/api/notes/1并接收以下内容:
{
"id":1,
"author":1,
"title":"Pierwsza notka",
"excerpt":"Taka tam notka, bla bla bla",
"body":"Pirwsza notka elo",
"private":1,
"created_at":"2021-04-07T12:59:59.000Z",
"updated_at":"2021-04-07T12:59:59.000Z"
}
Run Code Online (Sandbox Code Playgroud)
这很好,但是当我将它传递到 Next 时getStaticProps:
export async function getStaticProps({ params }) {
const res = await fetch(`${config.apiURL}/notes/${params.id}`);
const post = await res.json();
return { props: { post } };
}
Run Code Online (Sandbox Code Playgroud)
它返回一个错误:
FetchError: invalid json response body at http://localhost:3000/api/notes/1 reason: Unexpected end of JSON input
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
我创建了一个简单的 Express 应用程序,并使用 TypeScript 编译器将其编译为 TypeScript,然后运行 JS 编译的脚本。问题是,当我尝试实施时node-sass-middleware:
app.use(
sass({
src: join(__dirname, "..", "src", "style"),
dest: join(__dirname, "style"),
debug: true,
})
);
Run Code Online (Sandbox Code Playgroud)
__dirname等于 dist 文件夹。[sass] skip: / nothing to do
[sass] skip: /common/index.js nothing to do
Run Code Online (Sandbox Code Playgroud)
应用程序文件夹结构为:
src
- common
index.ts
- style
index.scss
app.ts
index.ts
server.ts
dist
- common
index.js
app.js
index.ts
server.ts
Run Code Online (Sandbox Code Playgroud)
有人可以解释如何正确使用 TypeScript 和 Express.js 以及 TypeScript 进行静态编译和 Sass 使用吗?拜托了。
如何为 Node.js 应用程序创建用户友好的安装?安装程序必须连接到 MySQL 数据库并执行命令。用户可以在不安装 Node.js 的情况下执行此操作吗?或者您有什么建议我该如何提供吗?SaaS 怎么样?我想过,但我害怕服务器和硬盘。node_modules 可能非常大。谢谢。
node.js ×3
reactjs ×3
express ×2
next.js ×2
javascript ×1
json ×1
sass ×1
typescript ×1
websocket ×1