Now we create index.js that will create koa HTTP server and user koa-router to route HTTP requests to our code.
var Koa = require('koa');
var Router = require('koa-router');
var app = new Koa();
var router = new Router();
router.get('/', (ctx, next) => {
ctx.body = 'Hello world';
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
We are done with coding and we want to install dependencies and start the server. npm start will start single instance of NodeJS that will handle our requests.
$ npm install
$ npm start
Lets use all CPUs available and create multiple NodeJS instance on the server.
$ pm2 start -i max index.js
[PM2][WARN] You are starting 0 processes in fork_mode without load balancing. To enable it remove -x option.
[PM2] Applying action restartProcessId on app [index](ids: 0,1,2,3,4,5,6,7)
[PM2] [index](1) ✓
[PM2] [index](0) ✓
[PM2] [index](2) ✓
[PM2] [index](3) ✓
[PM2] [index](4) ✓
[PM2] [index](5) ✓
[PM2] [index](6) ✓
[PM2] [index](7) ✓
[PM2] Process successfully started