JStagram – a simple example of an API-based web app for sharing pictures.
In this code, we are not doing tests, instead you should play with the JStagram example app. For inspiration, here is a list of things you might consider doing in JStagram:
- new feature: extend the data models with a function that returns a count of pictures (as shown in slides); extend the HTTP API so the count is available at /api/pictures/count, and extend the page to show the current count of pictures somewhere.
- fix: delete an uploaded file if it isn't an image (packages such as file_type or jimp may help here).
- new feature: add paging to the main page to go beyond the first 10 results (you might want to define query parameter
pagefor that) - new feature: add a per-picture page to show a single picture nicely
- new feature: look for a thumbnail package and process the image into full resolution for viewing and thumbnail for the index page
- new feature if you like databases: make title search full-text - databases have fulltext indexes and search functions
NB: if you're choosing an image processing package the benefit of choosing a native JS library is that it will work on any platform. Conversely the benefit of something that's compiles for a specific OS is native speed. We tend to favour portability for ease of development by many users.
To get the example running, you must install the source code and all modules and then run the server from the command line:
- To download the code, either use git (the simplest option):
git clone https://github.com/portsoc/ws_api.git
cd ws_apior download and unpack the zip which on linux can be achieved using
wget https://github.com/portsoc/ws_api/archive/master.zipthen
unzip master.zip
cd ws_api-master- To download any libraries the code uses, type:
npm install- Start the server by typing:
npm start- Visit your website.
- If you're on your VM you just need to put your VM's IP address into a browser, or if you're developing on a desktop machine it will be http://127.0.0.1:8080 .
-
Do the first two steps above
-
Change the mention of
model-inmemorytomodel-pgsqlinserver.js. -
Install and run PostgreSQL.
- If you're using your VM for this, PostgreSQL is already installed and running.
-
Edit
jstagram/config.jsonso that your database details are correct.- The defaults should work on your VM.
-
Install the database and tables using:
npm run initpgsql- If your database details differ from the defaults, you may need to update
package.jsonfor theinitpgsqlscript to work.
- If your database details differ from the defaults, you may need to update
-
Start the server by typing:
npm start- Visit your website.
- If you're on your VM you just need to put your VM's IP address into a browser, or if you're developing on a desktop machine it will be http://127.0.0.1:8080 .
If at all possible, we recommend you use git to download code rather than zips of a repository. This is preferable because if the repo is updated, then syncing those changes requires just one command (git pull) and usually any merging can be done automatically. Git is very powerful and we heartily encourages you to become familiar with it.