Skip to content

Instantly share code, notes, and snippets.

@basharh
Created September 13, 2017 18:06
Show Gist options
  • Save basharh/87539c06ccef2caed4a67614798523c6 to your computer and use it in GitHub Desktop.
Save basharh/87539c06ccef2caed4a67614798523c6 to your computer and use it in GitHub Desktop.
API Server
import express from 'express';
const session = require('express-session')
import graphqlHTTP from 'express-graphql';
import jwt from 'express-jwt';
import config from 'config';
import cookieParser from 'cookie-parser';
import { schema, root } from './schema';
import upload from './middleware/upload';
import auth from './middleware/authentication';
const app = express();
const jwtMiddleWare = jwt({
secret: config.get('auth.secret'),
getToken: (req) => req.cookies.auth_token,
credentialsRequired: false,
});
const checkAdminMiddleware = (req, res, next) => {
if (!(req.user && req.user.role === 'admin')) {
return res.status(401).send('unauthorized');
}
next();
}
app.use(session({ secret: 'keyboard cat', httpOnly: false }));
app.use(cookieParser());
app.use(jwtMiddleWare);
app.use('/imgs', express.static('uploads'));
app.use('/auth', auth);
app.get('/', function (req, res) {
res.send({
msg: 'Hello World!'
});
});
app.post('/upload', checkAdminMiddleware, upload.single('image'), function(req, res, next) {
res.end(req.file.key);
});
app.use('/graphql', graphqlHTTP({
schema,
rootValue: root,
graphiql: true,
}));
app.listen(3000, function () {
console.log('LAP API listening on port 3000!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment