SHARE
TWEET

Untitled

a guest Sep 17th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const path = require('path');
  3. const url = require('url');
  4. const fs = require('fs');
  5. const bodyParser = require('body-parser');
  6. const morgan = require('morgan');
  7. const multiparty = require('multiparty');
  8.  
  9. const db = require('./db');
  10.  
  11. const app = express();
  12.  
  13. app.use(bodyParser.json());
  14. app.use(bodyParser.urlencoded({extended: false}));
  15. app.use(morgan('dev'));
  16.  
  17. app.get('/api/folder/:id/files', function(req, res) {
  18.  
  19.   const data = {
  20.     items: db,
  21.   };
  22.  
  23.   res.status(200).json(data);
  24. });
  25.  
  26. app.post('/api/register', function(req, res, next) {
  27.   const form = new multiparty.Form();
  28.   form.parse(req, function(err, fields, files) {
  29.     Object.keys(fields).forEach(function(name) {
  30.       console.log('got field named ' + name + ' ' + fields[name]);
  31.     });
  32.  
  33.     Object.keys(files).forEach(function(name) {
  34.       console.log('got file named ' + name);
  35.     });
  36.  
  37.     console.log('Registration completed!');
  38.     res.status(403).send('Registration completed!');
  39.   });
  40. });
  41.  
  42. app.get('/', function(req, res) {
  43.   const filePath = path.join(process.cwd(), 'index.html');
  44.   res.sendFile(filePath);
  45. });
  46.  
  47. app.get('*', function(req, res) {
  48.   const filename = path.join(process.cwd(), url.parse(req.url).pathname);
  49.  
  50.   if (isDirectory(filename)) {
  51.     // if the url doesn't end with a slash, we need to redirect
  52.     if (req.url.charAt(req.url.length - 1) !== '/') {
  53.       res.writeHead(302, {'Location': req.url + '/'});
  54.       res.end();
  55.       return;
  56.     }
  57.  
  58.     const filePath = path.join(filename, 'index.js');
  59.     return res.sendFile(filePath);
  60.   }
  61.  
  62.   res.sendFile(filename);
  63. });
  64.  
  65. function isDirectory(path) {
  66.   try {
  67.     const stat = fs.lstatSync(path);
  68.     return stat.isDirectory();
  69.   } catch (e) {
  70.     // lstatSync throws an error if path doesn't exist
  71.     return false;
  72.   }
  73. }
  74.  
  75. module.exports = app;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top