Skip to content

distraught init

Michael Kramer edited this page Feb 21, 2018 · 2 revisions

Distraught Install


Package location https://www.npmjs.com/package/distraught

Check out the documentation for all the parts.

Important:

This works best if you install nodemon first!

.env value required

# Key used to sign cookies
SESSION_SECRET=12345567890

Distraught is a wrapper for expressjs it uses this SESSION_SECRET to encrypt the sessions.

How to set this up HTTPServer

const {httpServer, addCache} = require('distraught');
 
addCache('est', {connection: process.env.REDIS_URL}); // optional: if you want to use caching
 
const homeController = require('./controllers/home');
 
const server = httpServer({
  publicPath: path.join(__dirname, 'public'),
  viewPath: path.join(__dirname, 'views'),
  findUserById(id: number) {
    return cache.default.getOrSet(`user-${id}`, fetchUserById.bind(null, id)); // Needed for passport middleware
  },
});
 
server.app.use((req, res, next) => {
  // ...some middleware/plugin logic
  next();
});
 
/* WEB ROUTES */
server.app.get('/', homeController.get);
 
authController.setAuthRoutes(server.app, server.passport);
 
server.start();
  • Line 1: Adding the library

  • Line 3: Adding the caches

  • Line 5: The location to your Home Controller

  • Line 7: Create the server

  • Line 8: This is the location for all your public assests. ie: css, js, images

  • Line 9: The location of where the pug views.

  • Line 10-14: ???

  • Line 17-19: This is a the step 1 of the lifecycle of a web request.

  • Line 23: This is an example route.

  • Line 25: ???

  • Line 27: This will start the server

Folder stucture we are using

add this to your package.json.

"build-dev-structure": "mkdir server && cd server && mkdir web && cd web && mkdir controllers && mkdir public && mkdir views && mkdir middlewares && cd public && mkdir css && mkdir js && mkdir images"

Then run npm run build-dev-structure.

Making a web start.

Add this to the root index.js

const startWebServer = require('./web').startWebServer;

program
  .command('web')
  .description('start a web server')
  .action(startWebServer);