Create an application

Add the packaging commands, make a developer account, and write a small Node.js application.

Chapter 1 of 4 · about 4 minutes

In this chapter we will add the packaging commands to Nextweb, make ourselves a developer account, and write a small application with one page that says hello in our own words. By the end we'll have an application folder that Nextweb knows how to build.

We start in the nextweb-tour folder from Open Nextweb, with Nextweb running.

Add the packaging commands

The commands that build and publish packages come in a package of their own. Open the shell:

In your terminal
./nextweb

And install it:

At the nextweb prompt
pkg install nextweb-dev
You should see
Resolving dependencies...
   Architecture: x64 (auto-detected)
Installation order: nextweb-dev


Installing 1 package(s)...

Installing nextweb-dev...
  downloading
  verifying cache  [##########]  100%  517.9/517.9 KB
  extracting
  postinstall
  Adds pkg developer, pkg org, pkg create, pkg validate, pkg publish
nextweb-dev 0.1.3 installed successfully

Notice the second line under Installing nextweb-dev: five new commands, all under pkg.

Make a developer account

Every package has an owner, and its name starts with the owner's handle. We choose our handle once. It is ours for good, made of lowercase letters and digits:

At the nextweb prompt
pkg developer init --handle example
You should see
Created developer account example.
This instance's key 29b0cbae81c936b6 is approved.
Developer example (acc_5886c0cfd4eaa7ee)
  This key: 29b0cbae81c936b6 (approved)
  Package key: akek-2604fb770018
  Source encryption: not granted
  Certificate valid until 2026-10-14T01:28:30.696Z

The account belongs to our license. This copy of Nextweb now has its own key, and it is approved because it is the account's first. Each Nextweb folder has its own key, even a second folder on the same computer. When we run pkg developer init in another folder later, its key waits until it is approved: with the link Nextweb emails to the license's address, which works once for three days; under Developer keys in your account at user.nextwebjs.com, once the license is added there; or with pkg developer approve-key <key> from here. A copied license key alone can't publish in our name.

We leave the shell for now:

At the nextweb prompt
exit

Write the application

Back in the computer's terminal, make a folder for the application next to the nextweb program:

In your terminal
mkdir hello
cd hello

Our application is one file. Save this as hello/app.js:

hello/app.js
const http = require('http')

const host = process.env.NEXTWEB_HOST || '127.0.0.1'
const port = Number(process.env.NEXTWEB_PORT || 3000)

http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
  res.end('<h1>Hello from Example Company</h1>\n')
}).listen(port, host)

Nextweb tells the application where to listen through NEXTWEB_HOST and NEXTWEB_PORT. Your application on Nextweb lists everything else it is given. Next to it, save hello/package.json:

hello/package.json
{
  "name": "hello",
  "version": "0.1.0",
  "main": "app.js"
}

Describe it to Nextweb

Open the shell from inside the hello folder. Packaging works on the folder the shell was opened from:

In your terminal
../nextweb

Ask Nextweb to build our package. Its name is our handle, a dash, and a name of our choosing:

At the nextweb prompt
pkg create example-hello 0.1.0
You should see
Wrote a starter .nextwebrc. Review it (what to include, what to encrypt, who owns it), then run pkg create again.

Nothing is built yet. Nextweb looked at the folder, found a Node.js application, and wrote down what it understood in a new file, hello/.nextwebrc:

hello/.nextwebrc
{
  "type": "application",
  "distribution": "internal",
  "runtime": "node",
  "site": {
    "entry": "app.js",
    "verify": {
      "path": "/",
      "statusCode": 200
    }
  },
  "encrypt": {
    "source": []
  },
  "validate": {
    "endpoints": [
      {
        "path": "/",
        "status": 200
      }
    ]
  }
}
  • runtime says the application runs on Node.js, which Nextweb brings along.
  • site says how to run it as a website: start app.js, then check that / answers.
  • distribution is internal: the package installs only on our own license.
  • validate lists what pkg validate checks before we publish.

The .nextwebrc file describes every field.

One change makes the check ours. Add a contains line to the endpoint, so validation also proves the page says what we wrote:

hello/.nextwebrc
      {
        "path": "/",
        "status": 200,
        "contains": "Hello from Example Company"
      }

What we have now

A developer account named example, and a hello folder holding our application and a .nextwebrc that tells Nextweb how to build, run, and check it. Next, we'll build the package, publish it, and install it.