MSW(Mock Service Worker)从 8080 更改端口

MSW (Mock Service Worker) change port from 8080

MSW documentation for request-url 说:

// Only "POST https://api.backend.dev/users" requests match this handler
rest.post('https://api.backend.dev/users', responseResolver)
// Given your application runs on "http://localhost:8080",
// this request handler URL gets resolved to "http://localhost:8080/invoices"
rest.get('/invoices', invocesResolver)

搜索了很多但找不到有关如何更改端口的文档。所以它可能类似于 http://localhost:3001

上面强调了“localhost:8080”,因为相对于默认 localhost 的相对 URL 将解析为 localhost:8080/url。您不必自定义它。

亲戚URL

任何相对 URL 将相对于当前应用程序的位置。因此,如果您的应用程序是 http://localhost:3000 上的 运行,并且您定义了一个相对的 /user 处理程序,您将自动获得 http://localhost:3000/user

// If my app runs on :3000, the handler above intercepts
// a GET http://localhost:3000/user. I don't have to specify
// hostnames/ports for relative URLs against the application address.
rest.get('/user', resolver)

绝对URL

如果您(出于某种原因)需要为 外部 本地主机服务定义一个处理程序,不是您的应用程序, 你包括它的绝对 URL:

// My app runs on :3000 but I want to intercept and mock a request
// to another app that runs on localhost:3001. Here's how I do it:
rest.get('http://localhost:3001/user`, resolver)