Question

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.

Was it helpful?

Solution 2

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);

OTHER TIPS

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)

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}`,
      })
    );
  }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top