Git Product home page Git Product logo

contentful_express_tutorial's Introduction

This guide will walk you through your first steps using Contentful within an Express Node js application. It will provide a step-by-step guide on how to get your first entries and start using the content you create on Contentful.

Deploy

Requirements

  • A Heroku account
  • Heroku CLI installed
  • [Node.js] 2 6.2.1 installed
  • Npm 3.10.7 which should be installed with Nodejs
  • Git installed
  • Basic Command Line Interface knowledge

Start from a demo application

First we'll start with a demo project, which is a simple Express js application using Contentful's 'Product Catalogue' template, so that you can see it running for yourself.

Run the following commands to get started.

  • Clone the example repository:
git clone https://github.com/contentful/contentful_express_tutorial.git
  • Navigate into the repository's directory:
$ cd contentful_express_tutorial
  • Install dependencies:
$ npm install
  • Run the server:
$ npm run dev

Everything is now set up. You can view your new data by opening http://localhost:3000/ in your browser.

It should look something like this:

Express js Demo Application

Using your own content

You can create your own custom data by following these steps:

  • Create an account with Contentful or Log In.
  • Create a new Space with the 'Product Catalogue' template, name it whatever you like.
  • Copy the space Id and api key as shown in the screenshot

Keys Page

  • Change the product content type by your own, here. e.g. replacing 2PqfXUJwE8qSYKuM0U6w8M with product

  • Change the category content type by your own, here. e.g. replacing 6XwpTaSiiI2Ak2Ww0oi6qa with category

  • In the Express js application:

    • navigate to the package.json file open it and change the values accessToken and space in the config section and save.
    • run npm run dev to start the server
    • Your space will be now displayed in your application

Next in the Contentful web app > Content:

  • Open the product called 'Playsam Streamliner Classic Car, Espresso'.
  • Change the value of the Product name field to a new value,
  • Click the Publish changes button
  • Wait a few seconds for the changes to propagate to the CDN.
  • Reload your Express js application and you will see the new product name.

You can continue to edit your content inside the [Contentful web app][13] and see the content change inside your application.

Deploy the demo to Heroku

To view the demo application live in your own production environment, follow these steps:

  • Having Heroku CLI Installed, Login to Heroku if you're not logged in already:
heroku login
  • Create a new instance:
heroku create
  • Commit your change:
git add .
git commit -m "Add Website"
  • Deploy to Heroku:
git push heroku master
  • Open the application in your browser:
heroku open

Next Steps

After this guide, you should be able to start using Contentful with your Express js applications, but every project has different needs and we want to provide you with the best solutions we can.

You can read about the Contentful CDA library in more detail on our contentful.js GitHub or our Getting Started with CDA SDK tutorial. We also suggest taking a look at our Product Example Application.

Do you like building static sites? Check how to build static sites using Contentful with Metalsmith

contentful_express_tutorial's People

Contributors

cachrisman avatar cooper-kunz avatar khaledgarbaya avatar lucalanca avatar mariobodemann avatar sarce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

contentful_express_tutorial's Issues

Incorrect Content Type IDs

In line 7 and 14 of services/product.js the content type ID is set to '2PqfXUJwE8qSYKuM0U6w8M' instead of 'product'. This matches the default test space but in the express app demo, but once you change the auth token and space ID to your new example space using the Product Catalogue example space template, the app fails. The error also doesn't help at all. The Product Catalogue sample space uses 'product' for the content type ID, not '2PqfXUJwE8qSYKuM0U6w8M'. The express app demo requires this value to be 'product' but doesn't mention that the sample code has the wrong content type IDs. Same thing happens for categories.

I can't run the sample app as is, I keep getting this error

products.js - getProducts (line 17) error: {
  "sys": {
    "type": "Error",
    "id": "InvalidQuery"
  },
  "message": "The query you sent was invalid. Probably a filter or ordering specification is not applicable to the type of a field.",
  "details": {
    "errors": [
      {
        "name": "unknownContentType",
        "value": "DOESNOTEXIST"
      }
    ]
  },
  "requestId": "cec85c2a-3411-417e-b3a8-8a9c6811a33b"
}

Screen Shot 2019-11-05 at 12 03 51 PM

Heroku app fails to run

Playing around with Contentful and when I try to deploy to heroku, I get a 503 Application error. npm run production seems to generate errors in the heroku logs, but not sure how to resolve it. Can run npm start locally with no problem and can see the app at localhost:3000.

Upgrade from Jade to Pug

The team behind Jade realized someone else had the rights to that name. They've since renamed the package during a major version update to Pug.

You can read their comments here about the project's renaming. I'd be happy to submit a PR upgrading this example application to the new package, if that would be helpful?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.