Skip to main content
Browse Documentation
Technologies - JavaScript

Using Contensis with Express and Node.js

Created by r.bromley, last updated 11 Apr 2022

This step-by-step guide will take you through getting your entries from Contensis and displaying them using the Delivery API and a simple Node.js app.

Requirements

  • Node.js installed
  • NPM
  • Git
  • Command line interface knowledge

Using the demo project

This app will pull in data from the Leif project in Contensis.

To get started:

Clone the Contensis Express.js project

https://gitlab.zengenti.com/ps-projects/leif-example-sites/express-leif-example.git

Change directory to the repo directory

cd express-leif-example

Install dependencies

npm install

Run it

npm start

Go to http://localhost:3000 and view the Express app running in your browser.

How it works

Include the Contensis Delivery API helper

The Contensis Delivery API helper contains classes to perform the repetitive tasks of retrieving content from the API.

Include an instance of contensis-delivery-api in index.js:

const { Client, Query, Op } = require("contensis-delivery-api");

Connect to the Contensis Delivery API

Set the root URL of the Contensis CMS, access token, and project you want to use with the Delivery API.

const ContensisClient = Client.create({
  rootUrl: "<root-url>",
  accessToken: "<access-token>",
  projectId: "<project-id>",
});

Get a single blog entry by its ID

app.get("/blog/*", async (request, response) => {
  // Get the id from the query string
  const entryId = request.query.id;

  // Get the entry by id and set link depth to 1 to resolve linked images
  const entry = await ContensisClient.entries.get({ id: entryId, linkDepth: 1 });

  // Go to /views/blog.ejs for the HTML.
  response.render("blog", {
    blog: entry,
  });

});

Get a list of blogs

More information on search queries can be found here: https://www.contensis.com/help-and-docs/apis/delivery-js/search/query-operators

app.get(["/"], async (request, response) => {
  // Query the api for entries with a content type of "blogPost"
  // Get the latest versions even if not yet published
  const blogsQuery = new Query(
    Op.equalTo("sys.contentTypeId", "blogPost"),
    Op.equalTo("sys.versionStatus", "latest")
  );

  // Get a list of entries matching the blogsQuery
  const blogsPayload = await ContensisClient.entries.search(blogsQuery);

  // Go to /views/index.ejs for the HTML.
  response.render("index", {
    blogs: blogsPayload.items
  });

});
Necessary Cookies
These cookies are necessary for this website to function correctly. They are set when you perform certain actions on the site, such as creating an account, logging in, changing your privacy preferences or submitting a form. You can block these cookies in your browser, but this will stop parts of the site from working properly.
Functional Cookies
These cookies allow the website to provide extra functionality and more personalised experiences. They may be set by us or by third party providers whose services we have added to our pages. If you choose not to allow these cookies, these services may not work correctly.
Analytical Cookies
These cookies record anonymous data on how visitors use our website to help us monitor how well our website works. This data includes how many people have looked at specific pages, how long visitors stay on the site, and what devices they use. We use this data to identify changes that we could make to improve your experience and make our website more efficient.
Marketing Cookies
We set some cookies so you are shown more relevant marketing content. These include cookies from third-party advertising networks to show you different adverts on their services if you have previously visited our site. If you choose not to allow these cookies, you may experience less relevant advertising on other sites.