@dmail/server v2.10.0
Server
Simplified api to create server using node.js.
Introduction
@dmail/server is used to start a node.js server.
The main exports are:
startServerfirstServiceserveFile
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 contentresponse for/ - returns
200 okresponse 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.06 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago