Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
fix(global): inject the global variables into the server (#55)
Browse files Browse the repository at this point in the history
fix(global): inject the global variables into the server
  • Loading branch information
nahtnam authored Nov 12, 2019
2 parents 74eb68a + 958bd14 commit 943186d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions guides/global.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,19 @@ module.exports = handler(() => {
hello: 'world',
};
});
```

**Additionally, if you are using `light dev` or `light server`, the global variables get injected into the server under the `light` variable.** This is controversial because many people are against global variables and they are right, BUT if you do atomic actions such as logging or incrementing a counter, it shouldn't really matter.

```javascript
const { route } = require('light');

const { handler } = route();

module.exports = handler(() => {
light.logger.log('hello world!'); // no need to import anything
return {
hello: 'world',
};
});
```
3 changes: 3 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import findRoutes from './utils/find-routes';
import RouteType from './types/route';
import importRoutes from './utils/import-routes';
import addRoute from './utils/add-route';
import glob from './global';

interface Light {
server: Server;
Expand All @@ -23,6 +24,8 @@ const app = ({
routes: string | RouteType[];
opts?: any;
}): Light => {
const g = glob();
(global as any).light = g;
const router = Router({
ignoreTrailingSlash: true,
defaultRoute: (req: IncomingMessage, res: ServerResponse): void => {
Expand Down

0 comments on commit 943186d

Please sign in to comment.