[dev-server] Use dev-server as middleware for Express
See original GitHub issueI am using dev-server to serve a local project without having to build each time. In order for my application to actually work, I need to define routes within the server.
I wrote this tiny function which will basically enrich app
(which is an Express object) so that I add es-dev-server as Express routes. I do that by creating the server manually with createConfig, and then creating a Koa app, and use koaApp.callback()
to convert the koa app into an Express one. It works beautifully, and we are on the verge of releasing all of this under a free license.
However, as I understand es-dev-server is no more… I was hoping you could let me know if this code is still functional (that is, es-dev-server and es-server share the same API) or if I need to change things around. Or, if what I am doing can be done with es-dev-server.
Thank you for the awesome work.
exports = module.exports = function (app) {
const Koa = require('koa')
const config = esDevServer.createConfig({
nodeResolve: true,
appIndex: 'index.html',
moduleDirs: ['node_modules'],
preserveSymlinks: true
})
const middlewares = esDevServer.createMiddlewares(config)
const koaApp = new Koa()
middlewares.forEach(middleware => {
koaApp.use(middleware)
})
app.use(koaApp.callback())
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:17 (4 by maintainers)
One thing I don’t quite get… we have gorgeous ES6 which doesn’t require building or anything. I can understand Typescript in cases where you have a huge software project and typing actually helps, but… why typescript for something like the es-dev-server…? I guess it’s totally the dev’s choice, and it depends on what they are comfortable with. But I do wish I were looking at ES6 code right now.
Sorry about the noise. I wanted to add that I added literate documentation to the ~150 lines of code that make up the program.
Since you have vast experience in writing this, and if you have spare time, I would love to know where I can improve on it (e.g. use cases I didn’t predict,possible pitfalls, etc.). If not, all good.
If others are requesting such a feature (see: using web-dev-server as Express middleware), feel free to distribute/rebrand es6-dev-server as you like.
I hope this helped!
https://github.com/mobily-enterprises/es6-dev-server/blob/main/index.js