Example #1
0
import * as Koa from 'koa'
import * as views from 'koa-views'
import * as bodyParser from 'koa-bodyparser'
import * as http from 'http'
import * as path from 'path'

import router from './router'

import { connectDB } from './db'

const app = new Koa()

app.use(views(path.join(__dirname, '')))

app.use(bodyParser())

console.log('connecting database')
connectDB().then(() => {
    console.log('database connected')
    app.use(router.routes())
    http.createServer(app.callback()).listen(4000, () => {
        console.log(`http server listening on port: 4000`)
    })
})
Example #2
0
router.get('/people', getAllPeople);
router.get('/todos', getAllTodo);
router.get('/todos/:id', getTodo);
router.post('/todos', addTodo);
router.patch('/todos/:id', patchTodo);
router.delete('/todos/:id', deleteTodo);

// Read more about koa at http://koajs.com/
const app = new Koa();
app.use(cors());
app.use(logger());
app.use(bodyParser());
app.use(router.routes());

// Read more about koa views at https://github.com/queckezz/koa-views
// Read more about Nunjucks at https://mozilla.github.io/nunjucks/
const viewPath = path.join(__dirname, 'views');
app.use(views(viewPath, {
  map: {html: 'nunjucks'},
  options: {loader: new FileSystemLoader(viewPath)}
}));
app.use(async (ctx, next) => {
  // If nothing else was found, render index (assumption: single-page app)
  await ctx.render('index');
});

const port: (number|string) = process.env.PORT || 8080;
app.listen(port, () => {
  console.log(`Server is listening on port ${port}...`);
});
Example #3
0
 views(`${__dirname}/views`, {
   extension: "hbs",
   map: {
     hbs: "handlebars",
   },
   options: {
     partials: {
       "admin-menu": "./partials/admin-menu",
       card: "./partials/card",
       footer: "./partials/footer",
       header: "./partials/header",
       "html-foot": "./partials/html-foot",
       "html-head": "./partials/html-head",
       sidebar: "./partials/sidebar",
     },
     helpers: {
       eq: function fna(this: any, lvalue: any, rvalue: any, options: any) {
         if (arguments.length < 3) {
           throw new Error("Handlebars Helper equal needs 2 parameters");
         }
         if (lvalue !== rvalue) {
           return options.inverse(this);
         } else {
           return options.fn(this);
         }
       },
       neq: function fnb(this: any, lvalue: any, rvalue: any, options: any) {
         if (arguments.length < 3) {
           throw new Error("Handlebars Helper equal needs 2 parameters");
         }
         if (lvalue !== rvalue) {
           return options.fn(this);
         } else {
           return options.inverse(this);
         }
       },
     },
   },
 })
Example #4
0
import * as Koa from 'koa'
import * as views from 'koa-views'
import * as bodyParser from 'koa-bodyparser'
import * as http from 'http'
import * as path from 'path'

import router from './router'

const app = new Koa()

app.use(views(path.join(__dirname, ''), { extension: 'ejs'}))

app.use(bodyParser())

app.use(router.routes())

http.createServer(app.callback()).listen(3000, () => {
    console.log(`http server listening on port: 3000`)
})
Example #5
0
File: index.ts Project: Kpyu/AntCMS
// app.use(convert(bunyanLogger(logger, {
//   level: 'info',
//   timeLimit: 250
// })));


app.use(koaSession({ maxAge: 3000000, expires: new Date(Date.now() + 3000000) }));

app.use(mongooseMiddleware);

app.use(favicon(Path.join(__dirname, '..', 'favicon.ico')));

// 添加ejs视图解析器
app.use(views(Path.resolve(__dirname, '../', 'views'), {
  map: {
    html: 'ejs',
  },
}));


// 添加静态资源服务中间件
app.use(serve(Config.static.directory));


// 添加assets管道
app.use(pipeLine({
  manifest: Path.join(__dirname, '../', 'manifest.json'),
  prepend: '',
}));

// 添加各种中间件
import * as Koa from "koa";
import * as views from "koa-views";

const app = new Koa();

app.use(views('/views', {
    map: {
        html: 'underscore'
    },
    extension: '.html',
    engineSource: {},
    options: {}
}));

app.use((ctx: Koa.Context) => {
    ctx.render('user', {
        user: '******'
    }).then(() => console.log('done render call'));
});
Example #7
0
import { fa } from '../../misc/fa';
import config from '../../config';
import Note, { pack as packNote } from '../../models/note';
import getNoteSummary from '../../misc/get-note-summary';
const consts = require('../../const.json');

const client = `${__dirname}/../../client/`;

// Init app
const app = new Koa();

// Init renderer
app.use(views(__dirname + '/views', {
	extension: 'pug',
	options: {
		config,
		themeColor: consts.themeColor,
		facss: fa.dom.css()
	}
}));

// Serve favicon
app.use(favicon(`${client}/assets/favicon.ico`));

// Common request handler
app.use(async (ctx, next) => {
	// IFrameの中に入れられないようにする
	ctx.set('X-Frame-Options', 'DENY');
	await next();
});

// Init router