I'm new to koa.js library and I need some help. I'm trying to make simple REST application using koa.
I have a static html and javascript files I want to serve on route /
and REST API accessing from /api/
.
This is my project directory tree:
project
├── server
│ ├── node_modules
│ ├── package.json
│ └── src
│ ├── config
│ ├── resources
│ └── server.js
├── ui
│ ├── app
│ ├── bower.json
│ ├── bower_components
│ ├── dist
│ ├── node_modules
│ ├── package.json
│ └── test
This is my source:
var app = require('koa')();
app.use(mount('/api/places', require('../resources/places')));
// does not work
var staticKoa = require('koa')();
staticKoa.use(function *(next){
yield next;
app.use(require('koa-static')('../ui/app', {}));
});
app.use(mount('/', staticKoa));
// does not work
app.use(mount('/', function*() {
app.use(require('koa-static')('../ui/app/', {}));
}));
// does not work
app.use(mount('/', function*() {
app.use(require('koa-static')('.', {}));
}));
// GET package.json -> 404 not found
I've tried koa-static
, koa-static-folder
, koa-static-server
libraries and neither works so I'm doing something wrong.
I've tried this and it works, but I don't have access to my REST api:
var app = require('koa')();
app.use(require('koa-static')('../ui/app/', {}));