Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const path = require('path');
- const url = require('url');
- const fs = require('fs');
- const bodyParser = require('body-parser');
- const morgan = require('morgan');
- const multiparty = require('multiparty');
- const db = require('./db');
- const app = express();
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({extended: false}));
- app.use(morgan('dev'));
- app.get('/api/folder/:id/files', function(req, res) {
- const data = {
- items: db,
- };
- res.status(200).json(data);
- });
- app.post('/api/register', function(req, res, next) {
- const form = new multiparty.Form();
- form.parse(req, function(err, fields, files) {
- Object.keys(fields).forEach(function(name) {
- console.log('got field named ' + name + ' ' + fields[name]);
- });
- Object.keys(files).forEach(function(name) {
- console.log('got file named ' + name);
- });
- console.log('Registration completed!');
- res.status(403).send('Registration completed!');
- });
- });
- app.get('/', function(req, res) {
- const filePath = path.join(process.cwd(), 'index.html');
- res.sendFile(filePath);
- });
- app.get('*', function(req, res) {
- const filename = path.join(process.cwd(), url.parse(req.url).pathname);
- if (isDirectory(filename)) {
- // if the url doesn't end with a slash, we need to redirect
- if (req.url.charAt(req.url.length - 1) !== '/') {
- res.writeHead(302, {'Location': req.url + '/'});
- res.end();
- return;
- }
- const filePath = path.join(filename, 'index.js');
- return res.sendFile(filePath);
- }
- res.sendFile(filename);
- });
- function isDirectory(path) {
- try {
- const stat = fs.lstatSync(path);
- return stat.isDirectory();
- } catch (e) {
- // lstatSync throws an error if path doesn't exist
- return false;
- }
- }
- module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement