How to use koa-route - 10 common examples

To help you get started, we’ve selected a few koa-route examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zotero / translation-server / src / server.js View on Github external
const cors = require('./cors');

require('./zotero');
const Debug = require('./debug');
const Translators = require('./translators');
const SearchEndpoint = require('./searchEndpoint');
const WebEndpoint = require('./webEndpoint');
const ExportEndpoint = require('./exportEndpoint');
const ImportEndpoint = require('./importEndpoint');

const app = module.exports = new Koa();
app.use(cors);
app.use(bodyParser({ enableTypes: ['text', 'json']}));
app.use(_.post('/web', WebEndpoint.handle.bind(WebEndpoint)));
app.use(_.post('/search', SearchEndpoint.handle.bind(SearchEndpoint)));
app.use(_.post('/export', ExportEndpoint.handle.bind(ExportEndpoint)));
app.use(_.post('/import', ImportEndpoint.handle.bind(ImportEndpoint)));

Debug.init(process.env.DEBUG_LEVEL ? parseInt(process.env.DEBUG_LEVEL) : 1);
Translators.init()
.then(function () {
	// Don't start server in test mode, since it's handled by supertest
	if (process.env.NODE_ENV == 'test') return;
	
	var port = config.get('port');
	var host = config.get('host');
	app.listen(port, host);
	Debug.log(`Listening on ${host}:${port}`);
});
github zotero / translation-server / src / lambda.js View on Github external
const cors = require('./cors');
const serverless = require('serverless-http');

require('./zotero');
const Debug = require('./debug');
var Translators; // Translators module is cashed
const SearchEndpoint = require('./searchEndpoint');
const WebEndpoint = require('./webEndpoint');
const ExportEndpoint = require('./exportEndpoint');

const app = module.exports = new Koa();
app.use(cors);
app.use(bodyParser({enableTypes: ['text', 'json']}));
app.use(_.post('/web', WebEndpoint.handle.bind(WebEndpoint)));
app.use(_.post('/search', SearchEndpoint.handle.bind(SearchEndpoint)));
app.use(_.post('/export', ExportEndpoint.handle.bind(ExportEndpoint)));

Debug.init(1);

module.exports.handler = async function (event, context) {
	if (!Translators) {
		Translators = require('./translators');
		await Translators.init();
	}
	
	return await new Promise(function (resolve, reject) {
		serverless(app)(event, context, function (err, res) {
			if (err) return reject(err);
			resolve(res);
		});
	})
};
github zotero / translation-server / src / server.js View on Github external
require('./zotero');
const Debug = require('./debug');
const Translators = require('./translators');
const SearchEndpoint = require('./searchEndpoint');
const WebEndpoint = require('./webEndpoint');
const ExportEndpoint = require('./exportEndpoint');
const ImportEndpoint = require('./importEndpoint');

const app = module.exports = new Koa();
app.use(cors);
app.use(bodyParser({ enableTypes: ['text', 'json']}));
app.use(_.post('/web', WebEndpoint.handle.bind(WebEndpoint)));
app.use(_.post('/search', SearchEndpoint.handle.bind(SearchEndpoint)));
app.use(_.post('/export', ExportEndpoint.handle.bind(ExportEndpoint)));
app.use(_.post('/import', ImportEndpoint.handle.bind(ImportEndpoint)));

Debug.init(process.env.DEBUG_LEVEL ? parseInt(process.env.DEBUG_LEVEL) : 1);
Translators.init()
.then(function () {
	// Don't start server in test mode, since it's handled by supertest
	if (process.env.NODE_ENV == 'test') return;
	
	var port = config.get('port');
	var host = config.get('host');
	app.listen(port, host);
	Debug.log(`Listening on ${host}:${port}`);
});
github zotero / translation-server / src / server.js View on Github external
const _ = require('koa-route');
const bodyParser = require('koa-bodyparser');
const cors = require('./cors');

require('./zotero');
const Debug = require('./debug');
const Translators = require('./translators');
const SearchEndpoint = require('./searchEndpoint');
const WebEndpoint = require('./webEndpoint');
const ExportEndpoint = require('./exportEndpoint');
const ImportEndpoint = require('./importEndpoint');

const app = module.exports = new Koa();
app.use(cors);
app.use(bodyParser({ enableTypes: ['text', 'json']}));
app.use(_.post('/web', WebEndpoint.handle.bind(WebEndpoint)));
app.use(_.post('/search', SearchEndpoint.handle.bind(SearchEndpoint)));
app.use(_.post('/export', ExportEndpoint.handle.bind(ExportEndpoint)));
app.use(_.post('/import', ImportEndpoint.handle.bind(ImportEndpoint)));

Debug.init(process.env.DEBUG_LEVEL ? parseInt(process.env.DEBUG_LEVEL) : 1);
Translators.init()
.then(function () {
	// Don't start server in test mode, since it's handled by supertest
	if (process.env.NODE_ENV == 'test') return;
	
	var port = config.get('port');
	var host = config.get('host');
	app.listen(port, host);
	Debug.log(`Listening on ${host}:${port}`);
});
github zotero / translation-server / src / lambda.js View on Github external
const _ = require('koa-route');
const bodyParser = require('koa-bodyparser');
const cors = require('./cors');
const serverless = require('serverless-http');

require('./zotero');
const Debug = require('./debug');
var Translators; // Translators module is cashed
const SearchEndpoint = require('./searchEndpoint');
const WebEndpoint = require('./webEndpoint');
const ExportEndpoint = require('./exportEndpoint');

const app = module.exports = new Koa();
app.use(cors);
app.use(bodyParser({enableTypes: ['text', 'json']}));
app.use(_.post('/web', WebEndpoint.handle.bind(WebEndpoint)));
app.use(_.post('/search', SearchEndpoint.handle.bind(SearchEndpoint)));
app.use(_.post('/export', ExportEndpoint.handle.bind(ExportEndpoint)));

Debug.init(1);

module.exports.handler = async function (event, context) {
	if (!Translators) {
		Translators = require('./translators');
		await Translators.init();
	}
	
	return await new Promise(function (resolve, reject) {
		serverless(app)(event, context, function (err, res) {
			if (err) return reject(err);
			resolve(res);
		});
github nfl / react-wildcat / example / api / sampleServerOnlyApi.js View on Github external
module.exports = function(app /* , wildcatConfig*/) {
    console.info("Initializing server-only api...");

    const sampleRoute = route.get(
        "/react-wildcat-server-only-example",
        function*() {
            this.body = "Hello from the server world!";
        }
    );

    app.use(sampleRoute);
};
github Siedrix / paperpress / examples / koa / server.js View on Github external
const app = koa()
const paperpress = new Paperpress({
  baseDirectory: 'static',
  uriPrefix: '/blog',
  pathBuilder: function (item, collectionName) {
    if (collectionName === 'articles') {
      return `post/${item.slug}`
    } else if (collectionName === 'pages') {
      return `/page/${item.slug}`
    }
  }
})

paperpress.load()
app.use(route.get('/', list))
app.use(route.get('/feed', feed))
app.use(route.get('/page/:slug', page))
app.use(route.get('/post/:slug', show))

/** Route Definitions **/

// Post listing //
function * list (ctx) {
  this.body = yield paperpress.getCollection('articles')
  console.log('[koa-paperpress] /')
}

// Show post :slug //
function * show (slug) {
  const articles = paperpress.getCollection('articles')
  const article = _.findWhere(articles, {type: 'articles', slug: slug})
github hagata / 30daysofHelloWorld / Day-03-Koa / app.js View on Github external
const staticRoute = require('koa-static');
const parse = require('co-body');
// const render = require('./lib/render');
const views = require('co-views');
const path = require('path');

const app = module.exports = Koa();

//Temp Database
let db = [];

/**
 * Setup middleware
 */
app.use(staticRoute('./static'));
app.use(route.get('/', list));
app.use(route.get('/posts/new', add));
app.use(route.get('/post/:id', show));
app.use(route.post('/post', create));

/**
 * Setup route generators
 */
function* list() {
    console.log('DATABASE:', db)
    this.body = yield view('list', {posts: db});
}

function* add() {
    this.body = yield view('new');
}
github Setsun / react-koa-universal / src / server / middleware / useServeStatic.tsx View on Github external
const useServeStatic = (app) => {
  const servePublic = serve(path.join(__dirname, '../public'));

  // In production we want to serve our JS from a file on the filesystem.
  // Otherwise we want to proxy the webpack development server.
  const serveClient = process.env.NODE_ENV === 'production' ?
    serve(path.join(__dirname, 'dist/client')) :
    proxy({ target: 'http://localhost:8888', pathRewrite: { '^/static': '' } })

  app.use(route.get('/', servePublic));
  app.use(route.get('/static', serveClient));

  return app;
}
github soygul / koan / server / config / jwt.js View on Github external
jwt.routes = function (app) {
  app.use(route.post('/login', function *authenticate() {
    var credentials = yield parse(this);
    var user = yield mongo.users.findOne({email: credentials.email}, {email: 1, name: 1, password: 1});

    if (!user) {
      this.throw(401, 'Incorrect e-mail address.');
    } else if (user.password !== credentials.password) {
      this.throw(401, 'Incorrect password.');
    } else {
      user.id = user._id;
      delete user._id;
      delete user.password;
      user.picture = 'api/users/' + user.id + '/picture';
    }

    // sign and send the token along with the user info
    var token = jwt.sign(user, config.app.secret, {expiresInMinutes: 60 * 24 * 60});

koa-route

Koa route middleware

MIT
Latest version published 9 months ago

Package Health Score

72 / 100
Full package analysis

Popular koa-route functions