2.10.0 • Published 5 years ago

@dmail/server v2.10.0

Weekly downloads
19
License
MIT
Repository
github
Last release
5 years ago

Server

npm package build codecov

Simplified api to create server using node.js.

Introduction

@dmail/server is used to start a node.js server.

The main exports are:

  • startServer
  • firstService
  • serveFile

startServer Example

The following code starts a server listening to http://127.0.0.1:8080 responding Hello world as plain text.

import { startServer } from "@dmail/server"

startServer({
  protocol: "http",
  ip: "127.0.0.1",
  port: 8080,
  requestToResponse: () => {
    return {
      status: 200,
      headers: {
        "content-type": "text/plain",
      },
      body: "Hello world",
    }
  },
})

If you want to know more about startServer, there is a dedicated page documenting it. — see startServer documentation

serveFile example

The following code starts a server listening to http://127.0.0.1:8080 serving files of the current directory.

import { startServer, serveFile } from "@dmail/server"

startServer({
  protocol: "http",
  ip: "127.0.0.1",
  port: 8080,
  requestToResponse: ({ ressource, method, headers }) =>
    serveFile(`${__dirname}${ressource}`, {
      method,
      headers,
    }),
})

If you want to know more about serveFile, there is a dedicated page documenting it. — see serveFile documentation

firstService example

Let's make a generateResponse function with the following behaviour:

  • returns 204 no content response for /
  • returns 200 ok response for /whatever
import { firstService } from "@dmail/server"

const generateResponse = (request) => {
  return firstService(
    () => {
      if (ressource !== "/") return null
      return { status: 204 }
    },
    () => {
      if (ressource !== "/whatever") return null
      return { status: 200 }
    },
  )
}

If you want to know more about firstService, there is a dedicated page documenting it. — see firstService documentation

Installation

npm install @dmail/server@2.4.0
2.10.0

5 years ago

2.9.0

5 years ago

2.8.0

5 years ago

2.7.0

5 years ago

2.6.0

5 years ago

2.5.0

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.15.0

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago