This example shows how to implement a REST API using Express and Sutando. It uses a SQLite database file with some initial dummy data which you can find at ./example.db.
Clone this repository:
git clone git@github.com:sutandojs/sutando-examples.git --depth=1
Install npm dependencies:
cd sutando-examples/javascript/rest-express
npm install
Run the following command to create your SQLite database file. This also creates the users and posts tables that are defined in migrations:
npm run migrate
npm run seed
npm run start
The server is now running on http://localhost:3000. You can send the API requests implemented in index.js, e.g. http://localhost:3000/posts.
You can access the REST API of the server using the following endpoints:
/posts/:id: Fetch a single post by itsid/posts?searchString={searchString}&page={page}&perPage={perPage}&orderBy={orderBy}: Fetch all published posts- Query Parameters
searchString(optional): This filters posts bytitleorcontentpage(optional): This specifies which page should be returned toperPage(optional): This specifies how many objects should be returned per pageorderBy(optional): The sort order for posts in either ascending or descending order. The value can eitherascordesc
- Query Parameters
/users/:id/drafts: Fetch user's drafts by theirid/users: Fetch all users
/posts: Create a new post- Body:
title: String(required): The title of the postcontent: String(optional): The content of the postauthorEmail: String(required): The email of the user that creates the post
- Body:
/users: Create a new user- Body:
email: String(required): The email address of the username: String(optional): The name of the user
- Body:
/publish/:id: Toggle the publish value of a post by itsid/posts/:id/views: Increases theviews_countof aPostby oneid
/posts/:id: Delete a post by itsid
- Check out the Sutando docs
- Create issues and ask questions on GitHub