Comparing version 0.0.1-pre-11 to 0.0.1-pre-12
import { type Express, type Router } from 'express'; | ||
export declare const expressApp: (setup: (app: Express) => any) => (next: any) => Promise<void>; | ||
export declare const expressRouter: (setup: (router: Router) => any) => (next: any) => Promise<void>; | ||
export declare const expressApp: (setup: (app: Express) => any) => Promise<(next: any) => Promise<void>>; | ||
export declare const expressRouter: (setup: (router: Router) => any) => Promise<(next: any) => Promise<void>>; |
@@ -7,6 +7,6 @@ "use strict"; | ||
const jdefer_1 = require("jdefer"); | ||
const expressApp = (setup) => { | ||
const expressApp = async (setup) => { | ||
const symbol = Symbol('expressApp'); | ||
const app = (0, express_1.default)(); | ||
setup(app); | ||
await setup(app); | ||
app.use((req, _res, _next) => req[symbol].resolve()); | ||
@@ -24,6 +24,6 @@ app.use((err, req, _res, _next) => req[symbol].reject(err)); | ||
exports.expressApp = expressApp; | ||
const expressRouter = (setup) => { | ||
const expressRouter = async (setup) => { | ||
const symbol = Symbol('expressRouter'); | ||
const router = express_1.default.Router(); | ||
setup(router); | ||
await setup(router); | ||
router.use((req, _res, _next) => req[symbol].resolve()); | ||
@@ -41,2 +41,2 @@ router.use((err, req, _res, _next) => req[symbol].reject(err)); | ||
exports.expressRouter = expressRouter; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhwcmVzc0FwcC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9leHByZXNzQXBwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLHFDQUEwRDtBQUMxRCw2Q0FBNEQ7QUFFNUQsbUNBQThCO0FBRXZCLE1BQU0sVUFBVSxHQUFHLENBQUMsS0FBNEIsRUFBRSxFQUFFO0lBQzFELE1BQU0sTUFBTSxHQUFHLE1BQU0sQ0FBQyxZQUFZLENBQUMsQ0FBQTtJQUVuQyxNQUFNLEdBQUcsR0FBRyxJQUFBLGlCQUFPLEdBQUUsQ0FB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QU0sR0FBRyxNQUFNLENBQUMsZUFBZSxDQUFDLENBQUE7SUFFdEMsTUFBTSxNQUFNLEdBQUcsaUJBQU8sQ0FBQyxNQUFNLEVBQUUsQ0FBQTtJQUMvQixNQUFNLEtBQUssQ0FBQyxNQUFNLENBQUMsQ0FBQTtJQUNuQixNQUFNLENBQUMsR0FBRyxDQUFDLENBQUMsR0FBRyxFQUFFLElBQUksRUFBRSxLQUFLLEVBQUUsRUFBRSxDQUFDLEdBQUcsQ0FBQyxNQUFNLENBQUMsQ0FBQyxPQUFPLEVBQUUsQ0FBQyxDQUFBO0lBQ3ZELE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQyxHQUFHLEVBQUUsR0FBRyxFQUFFLElBQUksRUFBRSxLQUFLLEVBQUUsRUFBRSxDQUFDLEdBQUcsQ0FBQyxNQUFNLENBQUMsQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQTtJQUM5RCxPQUFPLEtBQUssRUFBQyxJQUFJLEVBQUMsRUFBRTtRQUNuQixNQUFNLEdBQUcsR0FBRywyQkFBYyxDQUFDLEtBQUssQ0FBQTtRQUNoQyxNQUFNLEtBQUssR0FBRyxJQUFBLGdCQUFTLEdBQUUsQ0FBQTtRQUN6QixHQUFHLENBQUMsTUFBTSxDQUFDLEdBQUcsS0FBSyxDQUFBO1FBQ25CLE1BQU0sQ0FBQyxHQUFHLEVBQUUsNEJBQWUsQ0FBQyxLQUFLLENBQUMsQ0FBQTtRQUNsQyxNQUFNLEtBQUssQ0FBQyxPQUFPLENBQUE7UUFDbkIsTUFBTSxJQUFJLEVBQUUsQ0FBQTtJQUNiLENBQUMsQ0FBQTtBQUNGLENBQUMsQ0FBQTtBQWZZLFFBQUEsYUFBYSxpQkFlekIifQ== |
import { type Express, type Router } from 'express'; | ||
export declare const expressApp: (setup: (app: Express) => any) => (next: any) => Promise<void>; | ||
export declare const expressRouter: (setup: (router: Router) => any) => (next: any) => Promise<void>; | ||
export declare const expressApp: (setup: (app: Express) => any) => Promise<(next: any) => Promise<void>>; | ||
export declare const expressRouter: (setup: (router: Router) => any) => Promise<(next: any) => Promise<void>>; |
import express from 'express'; | ||
import { requestContext, responseContext } from './context.js'; | ||
import makeDefer from 'jdefer'; | ||
export const expressApp = (setup) => { | ||
export const expressApp = async (setup) => { | ||
const symbol = Symbol('expressApp'); | ||
const app = express(); | ||
setup(app); | ||
await setup(app); | ||
app.use((req, _res, _next) => req[symbol].resolve()); | ||
@@ -19,6 +19,6 @@ app.use((err, req, _res, _next) => req[symbol].reject(err)); | ||
}; | ||
export const expressRouter = (setup) => { | ||
export const expressRouter = async (setup) => { | ||
const symbol = Symbol('expressRouter'); | ||
const router = express.Router(); | ||
setup(router); | ||
await setup(router); | ||
router.use((req, _res, _next) => req[symbol].resolve()); | ||
@@ -35,2 +35,2 @@ router.use((err, req, _res, _next) => req[symbol].reject(err)); | ||
}; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXhwcmVzc0FwcC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9leHByZXNzQXBwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sT0FBb0MsTUFBTSxTQUFTLENBQUE7QUFDMUQsT0FBTyxFQUFDLGNBQWMsRUFBRSxlQUFlLEVBQUMsTUFBTSxjQUFjLENBQUE7QUFFNUQsT0FBTyxTQUFTLE1BQU0sUUFBUSxDQUFBO0FBRTlCLE1BQU0sQ0FBQyxNQUFNLFVBQVUsR0FBRyxDQUFDLEtBQTRCLEVBQUUsRUFBRTtJQUMxRCxNQUFNLE1BQU0sR0FBRyxNQUFNLENBQUMsWUFBWSxDQUFDLENBQUE7SUFFbkMsTUFBTSxHQUFHLEdBQUcsT0FBTyxFQUFFLENBQUE7SUFDckIsS0FBSyxDQUFDLEdBQUcsQ0FBQyxDQUFBO0lBQ1YsR0FBRyxDQUFDL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QUFNLEdBQUcsT0FBTyxDQUFDLE1BQU0sRUFBRSxDQUFBO0lBQy9CLE1BQU0sS0FBSyxDQUFDLE1BQU0sQ0FBQyxDQUFBO0lBQ25CLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQyxHQUFHLEVBQUUsSUFBSSxFQUFFLEtBQUssRUFBRSxFQUFFLENBQUMsR0FBRyxDQUFDLE1BQU0sQ0FBQyxDQUFDLE9BQU8sRUFBRSxDQUFDLENBQUE7SUFDdkQsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDLEdBQUcsRUFBRSxHQUFHLEVBQUUsSUFBSSxFQUFFLEtBQUssRUFBRSxFQUFFLENBQUMsR0FBRyxDQUFDLE1BQU0sQ0FBQyxDQUFDLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFBO0lBQzlELE9BQU8sS0FBSyxFQUFDLElBQUksRUFBQyxFQUFFO1FBQ25CLE1BQU0sR0FBRyxHQUFHLGNBQWMsQ0FBQyxLQUFLLENBQUE7UUFDaEMsTUFBTSxLQUFLLEdBQUcsU0FBUyxFQUFFLENBQUE7UUFDekIsR0FBRyxDQUFDLE1BQU0sQ0FBQyxHQUFHLEtBQUssQ0FBQTtRQUNuQixNQUFNLENBQUMsR0FBRyxFQUFFLGVBQWUsQ0FBQyxLQUFLLENBQUMsQ0FBQTtRQUNsQyxNQUFNLEtBQUssQ0FBQyxPQUFPLENBQUE7UUFDbkIsTUFBTSxJQUFJLEVBQUUsQ0FBQTtJQUNiLENBQUMsQ0FBQTtBQUNGLENBQUMsQ0FBQSJ9 |
{ | ||
"name": "dx-server", | ||
"version": "0.0.1-pre-11", | ||
"version": "0.0.1-pre-12", | ||
"main": "./cjs/index.js", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:tranvansang/dx-server.git", |
216
README.md
@@ -12,7 +12,10 @@ # dx-server - modern, unopinionated, and satisfactory server | ||
Sample additionally requires: `yarn install express morgan` | ||
```javascript | ||
import {Server} from 'http' | ||
import {promisify} from 'util' | ||
import chain from 'jchain' | ||
import { | ||
requestContext, responseContext, | ||
makeContext, requestContext, responseContext, | ||
@@ -30,82 +33,145 @@ expressContext, setHtml, setJson, | ||
catchApiError, catchError, notFound, notFoundApi, | ||
expressApp, | ||
expressApp, expressRouter, | ||
} from 'dx-server' | ||
import express from 'express' | ||
import morgan from 'morgan' | ||
const tcpServer = new Server() | ||
.on('request', async (req, res) => { | ||
try { | ||
await chain( | ||
requestContext.chain(req), // required for most middlewares | ||
responseContext.chain(res), // required for most middlewares | ||
expressContext.chain({jsonBeautify: true}), // allows to use setHtml, setJson, setRaw, setBuffer, setFile, setRedirect, etc. | ||
bufferBodyContext.chain(), // use raw buffer body as Buffer use bufferBodyContext.value. This is required for jsonBodyContext, urlencodedBodyContext, textBodyContext, rawBodyContext | ||
jsonBodyContext.chain(), // to get body parsed as json use jsonBodyContext.value. Only available if content-type is application/json | ||
urlencodedBodyContext.chain(), // to get body parsed as urlencoded use urlencodedBodyContext.value. Only available if content-type is application/x-www-form-urlencoded | ||
textBodyContext.chain(), // to get body parsed as text use textBodyContext.value. Only available if content-type is text/plain | ||
rawBodyContext.chain(), // to get body as raw use rawBodyContext.value. Only available if content-type is application/octet-stream | ||
queryContext.chain(), // to get query params use queryContext.value. Query is parsed via 'qs' package. If no query, return empty object {} | ||
next => { | ||
// this is the difference between express and dx-server | ||
// req, res can be accessed from anywhere via context which uses NodeJS's AsyncLocalStorage under the hood | ||
responseContext.value.setHeader('cache-control', 'no-cache') | ||
next() | ||
}, | ||
async next => { | ||
// global error catching for all following middlewares | ||
try { | ||
await next() | ||
} catch (e) { | ||
// it is best practice to create custom error class for non-system error | ||
class ServerError extends Error { | ||
name = 'ServerError' | ||
constructor(message, status = 400, code = 'unknown') { | ||
super(message) | ||
this.status = status | ||
this.code = code | ||
} | ||
} | ||
// makeContext is a convenient way to create context | ||
const authContext = makeContext(() => { | ||
const req = requestContext.value | ||
// determine if user is authenticated | ||
// for e.g. | ||
if (req.headers.authorization) { | ||
return {id: 1, name: 'joe'} | ||
} | ||
}) | ||
const requireAuth = () => { | ||
if (!authContext.value) throw new ServerError('unauthorized', 401, 'unauthorized') | ||
} | ||
const serverChain = chain( | ||
expressContext.chain({jsonBeautify: true}), // allows to use setHtml, setJson, setRaw, setBuffer, setFile, setRedirect, etc. | ||
bufferBodyContext.chain(), // use raw buffer body as Buffer use bufferBodyContext.value. This is required for jsonBodyContext, urlencodedBodyContext, textBodyContext, rawBodyContext | ||
jsonBodyContext.chain(), // to get body parsed as json use jsonBodyContext.value. Only available if content-type is application/json | ||
urlencodedBodyContext.chain(), // to get body parsed as urlencoded use urlencodedBodyContext.value. Only available if content-type is application/x-www-form-urlencoded | ||
textBodyContext.chain(), // to get body parsed as text use textBodyContext.value. Only available if content-type is text/plain | ||
rawBodyContext.chain(), // to get body as raw use rawBodyContext.value. Only available if content-type is application/octet-stream | ||
queryContext.chain(), // to get query params use queryContext.value. Query is parsed via 'qs' package. If no query, return empty object {} | ||
next => { | ||
// this is the difference between express and dx-server | ||
// req, res can be accessed from anywhere via context which uses NodeJS's AsyncLocalStorage under the hood | ||
responseContext.value.setHeader('cache-control', 'no-cache') | ||
next() | ||
}, | ||
async next => { | ||
// global error catching for all following middlewares | ||
try { | ||
await next() | ||
} catch (e) { | ||
// only app error message should be shown to user | ||
if (e instanceof ServerError) setHtml(`${e.message} (code: ${e.code})`, {status: e.status}) | ||
else { | ||
// report system error | ||
console.error(e) | ||
setHtml('internal server error (code: internal)', {status: 500}) | ||
} | ||
} | ||
}, | ||
await expressApp(app => { | ||
// any express feature can be used | ||
// required express installed, with for e.g., `yarn add express` | ||
app.set('trust proxy', true) | ||
if (process.env.NODE_ENV !== 'production') app.set('json spaces', 2) | ||
app.use( | ||
morgan('common'), // in future, we will provide native implementation of express middlewares | ||
// cookies, session, etc. | ||
// session({ | ||
// secret: '123', | ||
// resave: false, | ||
// store: redisStore, | ||
// saveUninitialized: true, | ||
// // cookie: { secure: true } | ||
// }), | ||
) | ||
}), | ||
await expressRouter(router => { | ||
// setup express router | ||
router.use('/public', express.static('public')) | ||
}), | ||
authContext.chain(), | ||
// example of catching error for all /api/* routes | ||
router.post({ | ||
async '/api'({next}) { | ||
try { | ||
await next() | ||
} catch (e) { | ||
// only app error message should be shown to user | ||
if (e instanceof ServerError) setJson({ | ||
error: e.message, | ||
code: e.code, | ||
}, {status: e.status}) | ||
else { | ||
// report system error | ||
console.error(e) | ||
setHtml('internal server error', {status: 500}) | ||
setJson({ | ||
message: 'internal server error', | ||
code: 'internal' | ||
}, {status: 500}) | ||
} | ||
}, | ||
expressApp(app => { | ||
// any express feature can be used | ||
// required express installed, with for e.g., `yarn add express` | ||
app.use('/photos', express.static('photos')) | ||
}), | ||
// example of catching error for all /api/* routes | ||
router.post({ | ||
async '/api'({next}) { | ||
try { | ||
await next() | ||
} catch (e) { | ||
console.error(e) | ||
setJson({ | ||
message: 'internal server error', | ||
code: 'internal_server_error' | ||
}, {status: 500}) | ||
} | ||
} | ||
}, {end: false}), // note: {end: false} is required to match all /api/* routes. This option is passed directly to path-to-regexp | ||
router.post({ | ||
'/api/me'() { // sample POST router | ||
setJson({name: 'joe'}) | ||
} | ||
}), | ||
router.get({ // sample GET router | ||
'/'() { | ||
setHtml('ok') | ||
}, | ||
'/health'() { | ||
setHtml('ok') | ||
} | ||
}), | ||
router.post({ // api not found router | ||
'/api'() { | ||
setJson({ | ||
message: 'not found', | ||
code: 'not_found' | ||
}, {status: 404}) | ||
} | ||
}, {end: false}), | ||
() => { // not found router | ||
setHtml('not found', {status: 404}) | ||
}, | ||
)() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
}) | ||
} | ||
}, {end: false}), // note: {end: false} is required to match all /api/* routes. This option is passed directly to path-to-regexp | ||
router.post({ | ||
'/api/sample-public-api'() { // sample POST router | ||
setJson({name: 'joe'}) | ||
}, | ||
'/api/me'() { // sample private router | ||
requireAuth() | ||
setJson({name: authContext.value.name}) | ||
}, | ||
}), | ||
router.get({ // sample GET router | ||
'/'() { | ||
setHtml('ok') | ||
}, | ||
'/health'() { | ||
setHtml('ok') | ||
} | ||
}), | ||
router.post({ // api not found router | ||
'/api'() { | ||
throw new ServerError('not found', 404, 'not_found') | ||
} | ||
}, {end: false}), | ||
() => { // not found router | ||
throw new ServerError('not found', 404, 'not_found') | ||
}, | ||
) | ||
const tcpServer = new Server() | ||
.on('request', async (req, res) => { | ||
try { | ||
await chain( | ||
requestContext.chain(req), // required for most middlewares | ||
responseContext.chain(res), // required for most middlewares | ||
serverChain, | ||
)() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
}) | ||
const port = +(process.env.PORT ?? 3000) | ||
@@ -112,0 +178,0 @@ await promisify(tcpServer.listen.bind(tcpServer))(port) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
144203
186