Deploying Node.js Apps
Structure makes it easy to deploy Node apps. Any project with a `package.json` file is considered a Node application.
Overview
When you deploy a project with a package.json file, we'll first run `yarn install` to install any project dependencies you've defined. Where possible, we load these dependencies from our local node_modules cache to speed up build times.
Next, Structure will run `npm start` in your project directory to run your app. You'll need to define a `start` command in your `package.json` for your app to run properly.
Note that your Node app should expose and run on port 80. Other ports will not work.
Creating an Example App
Here, we'll go over what project files are required to deploy a minimal, but production-ready Node application on Structure. In this example, we'll create a simple Express app.
We'll need the create the following two files:
server.js
package.json
server.js
First, create a file called `server.js` that will initialize our Express app.
// File: server.js
const express = require('express');
// Create our app instance
const app = express();
// Define a route
app.get('/', (req, res) => {
res.send('Hello, world!\n');
});
// Listen on port 80
app.listen(80, '0.0.0.0');
console.log('Express application started!');
package.json
Next, create a `package.json` file to define our dependencies and our `npm start` command.
{
"name": "hello-world",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
Deploying
From here, you're ready to deploy to production! First, create a Node app on Structure (either through the web interface, or by running structure create
through the CLI). Let's assume your app is named "hello-world". Now, navigate to your project directory and run:
$ structure deploy --app hello-world
Note: When you deploy a new version of your app, you'll have to reload your app for the changes to take effect. You can run `structure restart --app APP-NAME` to do this.