This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "backend", | |
| "version": "1.0.0", | |
| "description": "a custom backend for a file upload website", | |
| "main": "server.js", | |
| "scripts": { | |
| "serve": "nodemon ." | |
| }, | |
| "repository": { | |
| "type": "git", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express') | |
| const session = require('express-session') | |
| const mongoose = require('mongoose') | |
| const logger = require('morgan') | |
| const bodyParser = require('body-parser') | |
| const busboy = require('connect-busboy') | |
| const MongoStore = require('connect-mongo')(session) | |
| const app = express() | |
| // ----------------------------------------------------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const router = require('express').Router() | |
| router.get('/', (request, response, next) => { | |
| return response.status(200).json({ | |
| "data": { | |
| "message": "API loaded" | |
| } | |
| }) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const File = require('../models/file') | |
| const Busboy = require('busboy') | |
| const fs = require('fs') | |
| exports.newFile = function (request, response, next) { | |
| var busboy = new Busboy({ headers: request.headers }) | |
| busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | |
| if (fs.existsSync(`../datum/${filename}`)) { | |
| return response.status(400).json({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const mongoose = require('mongoose') | |
| const fileSchema = new mongoose.Schema({ | |
| name: { type: String, required: true }, | |
| size: { type: Number, required: false }, | |
| location: { type: String, required: true }, | |
| mime: { type: String, required: true } | |
| }, { timestamps: true }) | |
| module.exports = mongoose.model('file', fileSchema) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const File = require('../models/file') | |
| const fs = require('fs') | |
| exports.getFile = async function (request, response) { | |
| let file = null | |
| try { file = await File.findById(request.params.id) } catch (err) {} | |
| if (!file) { | |
| return response.status(404).json({ | |
| "data": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| exports.getFiles = async function (request, response) { | |
| let files = await File.find({}) | |
| if (!files) { | |
| return response.status(404).json({ | |
| "data": { | |
| "message": "There are no uploaded files" | |
| } | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express') | |
| const session = require('express-session') | |
| const mongoose = require('mongoose') | |
| const logger = require('morgan') | |
| const bodyParser = require('body-parser') | |
| const busboy = require('connect-busboy') | |
| const MongoStore = require('connect-mongo')(session) | |
| const app = express() | |
| // ----------------------------------------------------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const router = require('express').Router() | |
| const uploads = require('../controllers/upload') | |
| const serve = require('../controllers/serve') | |
| router.get('/', (request, response, next) => { | |
| return response.status(200).json({ | |
| "message": "API loaded" | |
| }) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const mongoose = require('mongoose') | |
| const fileSchema = new mongoose.Schema({ | |
| name: { type: String, required: true }, | |
| size: { type: Number, required: false }, | |
| location: { type: String, required: true }, | |
| mime: { type: String, required: true } | |
| }, { timestamps: true }) | |
| module.exports = mongoose.model('file', fileSchema) |