Skip to content

Instantly share code, notes, and snippets.

@hieunguyendut
Created December 25, 2017 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hieunguyendut/657344e8817f12a66e6bf0ac8405d84a to your computer and use it in GitHub Desktop.
Save hieunguyendut/657344e8817f12a66e6bf0ac8405d84a to your computer and use it in GitHub Desktop.
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.use(function(req, res, next){
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