This tutorial goes through the steps of creating a simple Express.js application.
You'll benefit the most from it if:
You're just getting started with Node.js and Express.js and want an example of how to create a simple application using Express.js.
You're already familiar with Express.js application development but are interested in a better dev to production workflow, and don't like doing DevOps, or handing your application over when it's ready for production.
I'll be using Nanobox as it's the easiest way to get started with application development. It also ensures that, in the future, I won't have to worry about creating, configuring, and managing my applications infrastructure when it's ready for production. Which, for me, is a big deal.
Note: If you've never used Nanobox before, you'll first need to create a free account and download Nanobox Desktop. Also, Don't worry about whether or not you have Node.js or Express.js installed. You won't need them!
Create a Simple Express Application
Note: All nanobox commands must be run from the root of your project.
First, create the project folder and change into it:
mkdir nanobox-express && cd nanobox-express
A simple config file, called a
boxfile.yml defines your application's infrastructure. Nanobox uses it when creating any of your application's environments (development, staging, production, etc.)
At the root of your project, create a
boxfile.yml with the following:
run.config: engine: nodejs
Generate an Express.js App
Normally, you'd probably have Node.js and Express.js already installed and would have used the Express generator to create your app. I'll show you how to do it without having them installed (since thats one of the great things about Nanobox). You can easily dive into any new language or framework without having to get your local environment setup just right beforehand.
This will read your
boxfile.yml, create an isolated development environment, and drop you into a console within that environment.
nodejs engine specified in your
boxfile.yml installs Node.js and NPM (or yarn) automatically, so you'll just need to install Express:
# install express.js and save to package.json npm install express --save # install the express generator npm install express-generator -g # build an express skeleton express # Answer 'y' when notified the destination is not empty # install express npm packages npm install # exit the console exit
To allow connections from the host machine into the app’s container, configure your app to listen on all available IP’s (
0.0.0.0) by modifying
Now, using the
nanobox dns command, add a DNS alias so you can access your application from a browser without having to remember the IP that Nanobox generates for you:
nanobox dns add local express.dev
Run Your App
You can run your app using one of two ways.
If you're still inside of the nanobox console you can simply:
npm start # OR yarn start
If you've exited outside of the console already, you can pass your app's start command with
nanobox run npm start # OR nanobox run yarn start
Thats it! You're application should be running and you can visit it at express.dev:3000 (or whatever alias you created above)
Subscribe to Nanobox
Get the latest posts delivered right to your inbox