First, let's start a simple server in our index.js entry file. In the following code block, we simply require Koa as a dependency, then start a Koa server by using the app.listen() method:
// ./index.js
const Koa = require('koa');
const app = new Koa();
const port = process.env.PORT || 3000;
app.listen(port, () =>
console.log(`Server running on http://localhost:${port}`)
);
The server either runs on a specified port with the PORT environmental variable, or on the default 3000 port.
To start our application, you can run the following command from the project root:
node index.js
You should see the following message in your console after running this: Server running on http://localhost:3000.
To start the application on a different port, you can specify the following PORT environment variable when starting the application:
PORT=1234 node index.js
Running this will produce the following message: Server running on http://localhost:1234.