Jest.js MSW(模拟服务工作线程)将端口从8080更改为

cbeh67ev  于 2022-12-28  发布在  Jest
关注(0)|答案(1)|浏览(147)

request-url的MSW文档说明:

// 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的内容

pgvzfuti

pgvzfuti1#

上面强调了“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

如果您需要(出于某种原因)为 external localhost服务定义一个处理程序,而该服务 * 不是 * 您的应用程序,则应包括其绝对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)

相关问题