Introduction to Koa Js

  • npm is a package manager for node. The npm registry is a public library of open-source code packages for Node.js, front-end websites, mobile apps, robots, routers, and a variety of other JavaScript needs. npm gives us access to all of these packages and allows us to install them locally.
  • Using the following command you can check the installed version on node and npm.
> node --version
> npm --version
  • You will get output like this,
  • Here I used vs code to run this program. Create a new folder named hello-world.
  • Open new ‘Terminal’ and run the following code to create a package.json file,
npm init
  • It will request the following information from you.
  • To install koa run the following code,
npm install koa
  • To install nodemon run the following code,
npm install nodemon --save-dev
  • Create a new file called app.js and type the following code,
var koa = require('koa');
var app = new koa();

app.use(function* (){
this.body = 'Hello world!';
});

app.listen(3000, function(){
console.log('Server running on https://localhost:3000')
});
  • Save the file and type the following code in terminal,
nodemon app.js
  • This will start the server. To test this app, go to https://localhost:3000 in your browser and you should see the following message.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store