simonapiz / codestrips Goto Github PK
View Code? Open in Web Editor NEWBuild an API for Codestrips, a small application to allow users to create and save small one-pane comic strips.
Build an API for Codestrips, a small application to allow users to create and save small one-pane comic strips.
PORT
In app.js, add a new route to your application. The new route should monitor the /strips
endpoint for POST requests.
When a POST /strips request arrives, the application should validate the strip and send a 400 response if it is invalid.
The new Strip will arrive as a strip
property on the request body. Here is an example req.body.strip
:
{
head: 'happy',
body: 'plus',
background: 'boat'
bubbleType: 'statement',
bubbleText: 'Hello, world!',
caption: 'Test strip'
}
head
,body
,background
, andbubbleType
are required.bubbleText
andcaption
have default values (empty string), so they do not need to be validated by the server before being sent to the database in this case. Send a 400 status code if any of the required values are not present in the request.
Your POST /strips route should INSERT
a new strip into the database using the req.body.strip
values.
In your INSERT
callback, if an error occurs, send back a 500 response status.
Find the newly-created strip if no error occurred. You’ll have to get the proper row from the database with another query.
Set a 201 status code and the send the created strip inside the callback of your db.get()
. Create an object to send in the response and set its strip
property equal to the strip returned from the database. Send this object in the response.
Import your SQLite database into app.js.
Be sure to use
new sqlite3.Database(process.env.TEST_DATABASE || './db.sqlite');
for your database variable in order for tests to run correctly.
In app.js, add a new route to your application. This new route should monitor the /strips
endpoint for GET requests.
When a GET request is sent to /strips
, get an array of all strips from the Strip table.
Send back the array of all strips in the db.all()
callback. Create an object to send in the response and set its strips
property equal to the rows returned from the database.
If you ran the
seed.js
script, make sure your GET route behaves as expected by restarting your server and refreshing the web browser component. At the bottom of the page, you should see and be able to click on the names and view the strips from yourStrip
table!
Install sqlite3
. We’re going to need to use an older version of sqlite3, so use the following command:
npm install sqlite3@^3
Inside sql.js, import the sqlite3
library.
Still in sql.js, create a const db
and create a new sqlite3.Database
at './db.sqlite'
.
Use your db
to create a table called Strip
with the following schema:
id
, an integer as the primary key.head
, a not-null text column.body
, a not-null text column.background
, a not-null text column.bubble_type
, a non-null text column.bubble_text
, a not-null text column that defaults to an empty string.caption
, a not-null text column that defaults to an empty string. Run the file to create an empty Strip table by running the node sql.js
command in your terminal.
After creating the database, you can add two test strips to the database by running
node seed.js
in the terminal. If your database has been set up correctly, you will see a message logging that rows have been inserted intoStrip
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.