DankParrot

express.js sample

Aug 22nd, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3.  
  4. const serveIndex = require('serve-index');
  5.  
  6. // Error Handling
  7. app.use(function(err, req, res, next) {
  8.     console.error(err.name + ": " + err.message);
  9.     res.status(500).send("Error 500: Internal Server Error");
  10. });
  11.  
  12. // Logging
  13. app.use(function(req, res, next) {
  14.     console.log(req.method + ": '" + req.originalUrl + "'\t(" + req.ip + ")");
  15.     next();
  16. });
  17.  
  18.  
  19. // Static Files
  20. app.use(express.static('public'));
  21.  
  22. app.use(serveIndex('public', {
  23.     icons: true,
  24.     view: 'details',
  25.     stylesheet: 'content/style.css',
  26.     template: 'content/directory.html'
  27. }));
  28.  
  29.  
  30. // Error 404
  31. app.use(function(req, res, next) {
  32.     console.warn("Error 404: '" + req.path + "'");
  33.     res.status(404).send('Error 404: File Not Found');
  34. });
  35.  
  36.  
  37. app.listen(80, function() {
  38.     console.log("Listening on port 80.");
  39. });
Advertisement
Add Comment
Please, Sign In to add comment