小编Mah*_*dra的帖子

如何将 msw 与 Nextjs 13.2.1 一起使用(错误:无法访问服务器上的worker.start。)

我正在集成一个模拟 API,它基本上发送一个响应对象,其中包含一组应该显示在 UI(聊天框)中的消息以及用户名、用户图片等。

由于重大更改,我在设置msw 1.1.0with Next JS 13.2.1 (带有布局的实验应用程序目录)时遇到了一些麻烦。然而这是我的代码。

到目前为止我的代码是这样设置的

src/app/(用户)/live-view/page.tsx

import React from "react";
import ThreadPostList from "../../components/threadList";
 import { worker } from '../../../mocks/browser';

if (process.env.NODE_ENV === 'development') {
  worker.start();
}

async function getMessages() {
  const response = await fetch("http://localhost:3000/api/messages");
  // const data = await response.json();
  return response;
}


async function LiveViewPage() {
  const messages = await getMessages();
  const convertedMessages = Object.keys(messages);
  // console.log(convertedMessages, "ConvertedMessages");
  
  Object.values(messages).forEach(val => console.log(val));

  
  return (
    <div className="border-t border-gray-200 py-4 divide-y divede-gray-200"> …
Run Code Online (Sandbox Code Playgroud)

mocking typescript reactjs next.js msw

5
推荐指数
1
解决办法
3564
查看次数

标签 统计

mocking ×1

msw ×1

next.js ×1

reactjs ×1

typescript ×1