The node.js example app
The node.js Example App aims at getting across the very basics of how to work with our headless content management system and how to build apps using our officially supported JavaScript SDK. You’ll learn best practices for using the SDK to deliver content to your app and additionally learn some techniques for modelling your content in Contentful.
We hope this app will give you a better understanding of how decoupling content from its presentation enables greater flexibility and facilitates shipping higher quality software more quickly.
Contentful is a content management platform for web applications, mobile apps and connected devices. It allows you to create, edit and manage content in the cloud and publish it anywhere via powerful API. Contentful offers tools for managing editorial teams and enabling cooperation between organizations.
Requirements
- Node 8
- Git
- Contentful CLI (only for write access)
Without any changes, this app is connected to a Contentful space with read-only access. To experience the full end-to-end Contentful experience, you need to connect the app to a Contentful space with read and write access. This enables you to see how content editing in the Contentful web app works and how content changes propagate to this app.
Common setup
You can clone the space for this example app to your own Contentful account by using our CLI tool.
http://localhost:3000?space_id=<YOUR_CLONED_SPACE_ID>&delivery_token=<YOUR_DELIVERY_TOKEN>&preview_token=<YOUR_PREVIEW_TOKEN>
If you do not have the Contentful CLI installed you can find instructions on installation and usage here. For more information on the content model check out this repo.
Once you’ve created a space, you can change the credentials in the variables.env. If you don’t feel like changing code immediately, you can also inject credentials via url parameters like so:
Clone the repo and install the dependencies.
git clone https://github.com/contentful/the-example-app.nodejs.git
npm install
Steps for read-only access
To start the express server, run the following
npm run start:dev
Open http://localhost:3000 and take a look around.
Steps for read and write access (recommended)
Step 1: Install the Contentful CLI
Step 2: Login to Contentful through the CLI. It will help you to create a free account if you don't have one already.
contentful login
Step 3: Create a new space
contentful space create --name 'My space for the example app'
Step 4: Seed the new space with the content model. Replace the SPACE_ID with the id returned from the create command executed in step 3
contentful space seed -s '<SPACE_ID>' -t the-example-app
Step 5: Head to the Contentful web app's API section and grab SPACE_ID, DELIVERY_ACCESS_TOKEN, PREVIEW_ACCESS_TOKEN.
Step 6: Open variables.env and inject your credentials so it looks like this
NODE_ENV=development
CONTENTFUL_SPACE_ID=<SPACE_ID>
CONTENTFUL_DELIVERY_TOKEN=<DELIVERY_ACCESS_TOKEN>
CONTENTFUL_PREVIEW_TOKEN=<PREVIEW_ACCESS_TOKEN>
PORT=3000
Step 7: To start the express server, run the following
npm run start:dev
Final Step:
Open http://localhost:3000?enable_editorial_features and take a look around. This URL flag adds an “Edit” button in the app on every editable piece of content which will take you back to Contentful web app where you can make changes. It also adds “Draft” and “Pending Changes” status indicators to all content if relevant.
Deploy to Heroku
You can also deploy this app to Heroku:
Use Docker
You can also run this app as a Docker container:
Step 1: Clone the repo
git clone https://github.com/contentful/the-example-app.nodejs.git
Step 2: Build the Docker image
docker build -t the-example-app.nodejs .
Step 3: Run the Docker container locally:
docker run -p 3000:3000 -d the-example-app.nodejs
If you created your own Contentful space, you can use it by overriding the following environment variables:
docker run -p 3000:3000 \
-e CONTENTFUL_SPACE_ID=<SPACE_ID> \
-e CONTENTFUL_DELIVERY_TOKEN=<DELIVERY_ACCESS_TOKEN> \
-e CONTENTFUL_PREVIEW_TOKEN=<PREVIEW_ACCESS_TOKEN> \
-d the-example-app.nodejs