Skip to content

Instantly share code, notes, and snippets.

@YonatanKra
Last active October 17, 2020 19:56
Show Gist options
  • Save YonatanKra/44e085409f6d88cb22f460818d277c44 to your computer and use it in GitHub Desktop.
Save YonatanKra/44e085409f6d88cb22f460818d277c44 to your computer and use it in GitHub Desktop.
AskQL powered express server example
import express from 'express';
import bodyParser from 'body-parser';
import {logRequestMiddleware, logResultsMiddleware, logError} from "./logger/logger.js";
import middlewareFactory from "askql/askExpressMiddleware/askExpressMiddleware.js";
import askql from 'askql';
const {resources} = askql;
const values = {
names: ['Johnny', 'Rita', 'Jane', 'Martha', 'Clark'],
powers: ['Nerd', 'C#', 'Cool', 'Superman\'s mom', 'Superman']
};
const { askExpressMiddleware } = middlewareFactory;
const askMiddleware = askExpressMiddleware({ resources, values }, { callNext: true, passError: true });
const port = 8080;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.post('/ask', [logRequestMiddleware, askMiddleware, logResultsMiddleware, logError]);
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment