0

I'm hosting an emberjs application on koajs with koa-static. according to below code :

var app = koa();
var App1 = koa();
App1.use(stati(__dirname + "/App1"));
app.use(mount('/AppViewer',App1));
app.listen(3000); 

When calling localhost:3000/AppViewer i can't see my page and gets error says can't find .js and .css files, but when calling localhost:3000/AppViewer/index.html all things works correctly. so my question is how to see my page when calling localhost:3000/AppViewer.

MBehtemam
  • 7,865
  • 15
  • 66
  • 108

3 Answers3

2

This was sufficient for me using ember-cli and deploying to heroku:

var common = require('koa-common'),
    koa = require('koa'),
    app = koa();

var env = process.env.NODE_ENV || 'development',
    port = process.env.PORT || '3800';

app.use(common.logger());
app.use(common.responseTime());
app.use(common.static(__dirname + "/dist"));

app.listen(port);

console.log('listening on port ' + port);

where /dist is the build output for ember build (equivalent of grunt dist w/ EAK)

Rikki Schulte
  • 186
  • 2
  • 7
1

First, it seems strange, because koa-static is supposed to default to serving index.html on root. Make sure App1 is not routing the / to somewhere else.

Then, you can still get rid of index.html in URL by using koa-rewrite. Something like:

var rewrite = require('koa-rewrite');
App1.use(rewrite('/*', '/index.html'));

UPDATE

Tested with the following code:

var koa = require('koa');
var app = koa();
var app1 = koa();
var mount = require('koa-mount');
var serve = require('koa-static');
var rewrite = require('koa-rewrite');
var route = require('koa-route');

app1.use(serve(__dirname + "/app1"));
app1.use(rewrite('/*', '/index.html'));
app.use(mount('/av', app1));

app.use(route.get('/', function *(next) {
  this.body = 'I\'m the main app!';
}));

app.listen(3000);
bredikhin
  • 8,875
  • 3
  • 40
  • 44
  • if i use `/app1/` its work but if i use `/app1` it dosent work – MBehtemam Feb 25 '14 at 05:30
  • Did you try `App1.use(rewrite('*', '/index.html'));` (no leading slash)? – bredikhin Feb 25 '14 at 07:41
  • Hmm, works for me, with or without trailing slashes. I'll update the answer with the code I used to test. – bredikhin Feb 25 '14 at 15:58
  • Actually, this didn't work for me when using routes with ember-cli. I found I had to disable the History API (which is what allows you to get arount using #s for routing). Also, read this: http://www.ember-cli.com/#history-api-and-base-url – Rikki Schulte Aug 11 '14 at 01:52
0

koa-static didn't work for me. I instead tried koa-static-server and yay!

My middleware:

var Proxy = require("koa-proxy");
// const Rewrite = require("koa-rewrite");
const StaticServer = require("koa-static-server");

export const InstallWebApp = (app) => {
  if (process.env.NODE_ENV === "production") {
    // Install static web app
    // app.use(Rewrite('/*', '/'));
    app.use(StaticServer({rootDir: `${__dirname}/../../../../client-web/build`, notFoundFile: "index.html"}));
  } else {
    // Install dev server
    app.use(
      Proxy({
        host: `http://localhost:${process.env.WEB_DEV_PORT}`,
      })
    );
  }
};
bdombro
  • 1,251
  • 11
  • 17