an example, originally for DBPRIN 2014, of how to put a database on the Web with Node.js
config.js
– configuration optionsstatic/*
– static pages and client codeserver.js
– MySQL-backed API serverserver-inmemory.js
– simple, in-memory version of the serverdatabase.sql
– the SQL commands to set up the databasepackage.json
– defines the dependencies of this package.php-version
- a partial implementation of the API in PHP.
- On your VM, clone the source code for the server
git clone https://github.com/portsoc/Web-DB-with-Node.git
- In the directory
Web-DB-with-Node
, runnpm install
cd Web-DB-with-Node
npm install
- Start the in-memory server
npm start
- Point your browser to your VM's IP address, check that the online shop works as it did in the lecture
- Explore and tweak the webpages and scripts in
static/
to understand how they work
If you're also interested in the server code:
- Set up your MySQL database
- see
database.sql
for the code with the database schema and sample data - fill the username, password, and database name in
config.js
- Run
node server.js
instead ofnpm start
- to drop the delay for API calls (by default 1s), change the
1000
inconfig.js
to0
- if you've changed your
config.js
or any server source code, kill the server (e.g. withCtrl-C
) and start it again
- Make sure you have
node.js
andnpm
installed - Download or clone the source code for the server
git clone https://github.com/portsoc/Web-DB-with-Node.git
- In the directory with
package.json
, runnpm install
cd Web-DB-with-Node
npm install
- Set up your MySQL database
- see
database.sql
for the code with the database schema and sample data - fill the username, password, and database name in
config.js
- Run
node server.js
- if you want to change the port at which the server listens (the default is 8080), you can run
PORT=yournumber node server.js
or put your preferred port number inconfig.js
- to drop the delay for API calls (by default 1s), change the
1000
inconfig.js
to0
- if you've changed your
config.js
, kill the server (e.g. withCtrl-C
) and start it again
A subset of the API is presented using PHP to illustrate that the API is an interface and that the client can and should be completely agnostic and unaware of the technology that is fulfilling its requests.
e.g, Once the client is loaded, stop the node server, then start the PHP server as follows:
npm run startphp
Navigating around the page should still be possible as the content is provided by PHP in response to each category selection.