jeffijoe / koa-es7-boilerplate Goto Github PK
View Code? Open in Web Editor NEWA boilerplate for writing Koa 2 apps in ES7 with Babel.
License: MIT License
A boilerplate for writing Koa 2 apps in ES7 with Babel.
License: MIT License
I would like to remove the babel dependency and use the new ECMAScript modules support but it seems like a hard task.
What I did:
"type": "module"
in the package.json
..js
for every file import. An example:import { logger } from './logger'
into:
import { logger } from './logger.js'
__dirname
variable I added this code:import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
like suggested here: https://nodejs.org/api/esm.html#esm_no_require_exports_module_exports_filename_dirname
require
in the awilix npm module (https://github.com/jeffijoe/awilix/blob/master/src/container.ts#L584) and now I don't know what to do.Can someone help me about that?
Hey jeffi, long time no see xD
About to deploy my app and having issues trying to run it in prod. When I run npm run start (node dist/bin/server.js
), it keeps running it in development mode. Any clue what I need to do?
Does it support passport or jwt etc. for mobile based auth?
Thank you in advance.
Hi Joe,
Greetings of the day...!!!
We have created some generic service and we would like call the another service class function in this service.
and all services are registered under loaded under scoped.
When we are trying to loadthe service in the constructor function and using the same in our function getting below error.
→ TypeError: Cannot read property 'apply' of undefined
→ at memberInvoker (D:\UI\Projects\koa-abs\node_modules\awilix-koa\src\invokers.ts:93:38)
→ at dispatch (D:\UI\Projects\koa-abs\node_modules\koa-router\node_modules\koa-compose\index.js:44:32)
→ at next (D:\UI\Projects\koa-abs\node_modules\koa-router\node_modules\koa-compose\index.js:45:18)
→ at D:\UI\Projects\koa-abs\node_modules\koa-router\lib\router.js:346:16
→ at dispatch (D:\UI\Projects\koa-abs\node_modules\koa-router\node_modules\koa-compose\index.js:44:32)
→ at D:\UI\Projects\koa-abs\node_modules\koa-router\node_modules\koa-compose\index.js:36:12
→ at dispatch (D:\UI\Projects\koa-abs\node_modules\koa-router\lib\router.js:351:31)
→ at dispatch (D:\UI\Projects\koa-abs\node_modules\koa-compose\index.js:42:32)
→ at D:\UI\Projects\koa-abs\node_modules\koa-compose\index.js:34:12
→ at dispatch (D:\UI\Projects\koa-abs\node_modules\koa-compose\index.js:42:32)
→ at scopePerRequestMiddleware (D:\UI\Projects\koa-abs\node_modules\awilix-koa\src\scope-per-request.ts:13:12)
→ at dispatch (D:\UI\Projects\koa-abs\node_modules\koa-compose\index.js:42:32)
→ at bodyParser (D:\UI\Projects\koa-abs\node_modules\koa-bodyparser\index.js:86:11)
→ at
→ at process._tickDomainCallback (internal/process/next_tick.js:228:7)
Could you please provide any help in this regard.
node js support native async await after v7.4 better koa performance
I really liked the work done in the project, Its definitely one of the most interesting boilerplates out there. Unfortunately, there is a major draw back - which is lack of documentation. Not project documentation, but auto generated API documentation, something like Swagger, apidoc etc.
So my request is simple, if possible, please advise on how to integrate a tool like Swagger.
Thanks!
I've switched to using babel-preset-env and have it set up so it auto-detects what version of node I'm running and transpiles the code based on that.
It feels easier and more dynamic than having to gauge what version of node supports what version of ecmascript and at what stage.
example .babelrc
:
{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
]
}
Hello,
I have an issue with the current configuration when running "npm run dev"
sometimes when i save, i get multiple times:
[nodemon] restarting due to changes...
[nodemon] starting npm run babel-node src/bin/server.js
which causes issues, sometimes it crash as the app continue to runs on the same port.. then i got an exception "already running on port 1338"
I don't have any clue how to fix that, it's difficult to work on the project as each time i save I need to find the process by pid and kill it.
lsof -i tcp:1338
to find the process.
If you have any ideas.
Kr,
→ TSError: ⨯ Unable to compile TypeScript:
→ src/services/user-service.ts(2,10): error TS2305: Module '"../stores/user-store"' has no exported member 'UserStore'.
→
→ at createTSError (/home/damasceno/dev/forum-api/node_modules/ts-node/src/index.ts:230:12)
→ at getOutput (/home/damasceno/dev/forum-api/node_modules/ts-node/src/index.ts:336:40)
→ at Object.compile (/home/damasceno/dev/forum-api/node_modules/ts-node/src/index.ts:369:11)
→ at Module.m._compile (/home/damasceno/dev/forum-api/node_modules/ts-node/src/index.ts:415:43)
→ at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
→ at Object.require.extensions.(anonymous function) [as .ts] (/home/damasceno/dev/forum-api/node_modules/ts-node/src/index.ts:418:12)
→ at Module.load (internal/modules/cjs/loader.js:599:32)
→ at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
→ at Function.Module._load (internal/modules/cjs/loader.js:530:3)
→ at Module.require (internal/modules/cjs/loader.js:637:17)
→ at require (internal/modules/cjs/helpers.js:22:18)
→ at Object._loadModulesDeps.require (/home/damasceno/dev/forum-api/node_modules/awilix/src/container.ts:553:18)
→ at modules.map.m (/home/damasceno/dev/forum-api/node_modules/awilix/src/load-modules.ts:100:33)
→ at Array.map (<anonymous>)
→ at Object.loadModules (/home/damasceno/dev/forum-api/node_modules/awilix/src/load-modules.ts:92:26)
→ at Object.loadModules (/home/damasceno/dev/forum-api/node_modules/awilix/src/container.ts:558:5)
This is my user-store.ts
:
import UserSchema, { User } from '../models/user-model';
export default class UserStore {
logger: any;
constructor(logger: any) {
this.logger = logger;
}
async find() {
this.logger.debug('Getting all users');
return await UserSchema.find();
}
async findOneById(id: String) {
this.logger.debug(`Getting user with id ${id}`);
return await UserSchema.findById(id);
}
async create(user: User) {
this.logger.debug(`Creating user with id ${user.id}`);
return await new UserSchema(user).save();
}
async update(id: String, user: User) {
this.logger.debug(`Updating user with id ${id}`);
return await UserSchema.findByIdAndUpdate(id, user, { new: true });
}
async remove(id: String) {
this.logger.debug(`Removing user with id ${id}`);
return UserSchema.findByIdAndRemove(id);
}
}
And this is my container.ts
import { Lifetime, InjectionMode, createContainer, asValue, AwilixContainer } from 'awilix';
import { logger } from './logger';
const modulesToLoad: any = [
['services/*.ts', Lifetime.SCOPED],
['stores/*.ts', Lifetime.SINGLETON]
];
/**
* @returns {AwilixContainer} The container
*/
export function configureContainer(): AwilixContainer {
const options: any = {
injectionMode: InjectionMode.CLASSIC
};
return createContainer(options)
.loadModules(modulesToLoad, {
cwd: `${__dirname}/..`,
formatName: 'camelCase'
})
.register({
logger: asValue(logger)
});
}
You can see the complete code here forum-api
I think your boilerplate is awesome!!.
But, I want to know the details and principles of your code.
Hi,
Sorry to bother you, but still a bit unclear on the purpose of the invocation.js/invoker methods. I understand its for the AwilixContainer object where it will all resolve (i think?), but in the context of this template, do we simply reuse the one given to us? Or does (typically) require a a new one to be created for each service or api we make?
As of koa-cors 2.x it's compatible with Koa 2, so we no longer need to include koa-convert. Also, can set Koa to require straight 2.0.0 instead of 2.0.0-alpha.
Hi,
It wasn't really clearly stated, but if I wanted to import other node modules, would I put it in the lib/createServer.js
or whatever service file I make (ie. services/myWhateverService.js
)? If it's for a database connections or something used globally, would it be better to import it in the former?
Hi Jeffijoe,
Greetings of the day... !!!
We are trying to generate the Swagger api for the repository to expose the apis.
Could you please suggest what are the changes we need to consider for this.
Thanks
Hi, I have used this boilerplate to build 2 apis and love it so much. However, I am not able to deploy to heroku. It returns this error all the time.
I did try to install source-map or source-map/register by using Heroku's console but it does not work. My deploy method is using GitHub and I already push the folder "dist" to GitHub but doesn't change anything.
https://github.com/jeffijoe/koa-es7-boilerplate/blob/master/src/lib/configureContainer.js#L18
Needs the __dirname as cwd, else prod build will fail.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.