Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A minimal but customizable Express server for testing

License

NotificationsYou must be signed in to change notification settings

b4dnewz/express-test-server

Repository files navigation

A minimal but customizable Express server for testing

NPM versionBuild StatusDependency StatusCoverage percentage

Typescript based preconfigured Express application intended for quick testing requests and responses, it can can be customized to listen for HTTP and HTTPS traffic and alter the default body parser behavior.

The followingContent-Type headers will be parsed and exposed viareq.body:

  • JSON (application/json)
  • Text (text/plain)
  • URL-Encoded (application/x-www-form-urlencoded)
  • Buffer (application/octet-stream)

Installation

npm install --save-dev @b4dnewz/express-test-server

Getting started

importcreateServerfrom"@b4dnewz/express-test-server"constserver=awaitcreateServer({// server options});// Express route handlerserver.get('/foo',(req,res)=>{res.send('bar');});// Express alternative route handlersserver.get('/bar',()=>'foo');server.get('/baz','foo');

Perfect for testing

importcreateServerfrom"@b4dnewz/express-test-server"letserver;beforeAll(async()=>{server=awaitcreateServer();});afterAll(async()=>{awaitserver.close();});it("respond to get requests",async()=>{sever.get("/foo","bar")const{body}=awaitgot(`${server.url}/foo`)expect(body).toEqual("bar")})

Options

port (default 0)
Specify a custom port for the HTTP server instance, otherwise it will automatically choose a random free TCP port

awaitcreateServer({port:8888})

sslPort (default443)
Specify a custom port for the HTTPS server instance, otherwise it will try to default ssl port

awaitcreateServer({sslPort:4443})

hostname (defaultlocalhost)
Specify a custom hostname for both HTTP and HTTPS servers, remember that you need a resolvable DNS host name for this to work.

awaitcreateServer({hostname:"0.0.0.0"})awaitcreateServer({hostname:"test.example.com"})

listen (defaulttrue)
If false will prevent the test server to automatically start to listen for requests when instanciated.

constserver=awaitcreateServer({listen:false})// listen later on a desired portawaitserver.listen({port:8888})

License

MIT

About

A minimal but customizable Express server for testing

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp