Created
December 25, 2017 14:53
-
-
Save nghuuquyen/a61cd913c8673b659ccd6bcabeb1945b to your computer and use it in GitHub Desktop.
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 fileUpload = require('express-fileupload'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
// default options | |
app.use(fileUpload()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.use(bodyParser.json()); | |
app.use(function (req, res, next) { | |
// Website you wish to allow to connect | |
res.header('Access-Control-Allow-Origin', '*'); | |
// Request methods you wish to allow | |
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); | |
// Request headers you wish to allow | |
res.header('Access-Control-Allow-Headers', 'X-Requested-With,content-type, app-key, verify-token'); | |
// Set to true if you need the website to include cookies in the requests sent | |
// to the API (e.g. in case you use sessions) | |
res.header('Access-Control-Allow-Credentials', true); | |
// Pass to next layer of middleware | |
next(); | |
}); | |
app.all("/api/*", function(req, res, next) { | |
console.log(req.method); | |
console.log(req.url); | |
console.log(req.headers); | |
console.log(req.body); | |
if(req.headers['app-key'] !== "CopyXwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT") { | |
return res.sendStatus(404); | |
} | |
next(); | |
}); | |
app.post('/api/signup', function(req, res) { | |
console.log(req.headers['verify-token']); | |
console.log(req.headers['app-key']); | |
if (!req.files) | |
return res.status(400).send('No files were uploaded.'); | |
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file | |
let avatar = req.files.avatar; | |
// Use the mv() method to place the file somewhere on your server | |
avatar.mv('./nana.jpg', function(err) { | |
if (err) | |
return res.status(500).send(err); | |
res.send('File uploaded!'); | |
}); | |
}); | |
app.listen(3001, () => console.log("Server running at port: " + 3001)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment