This repo contains exemplary usage of javascript:
- Loading JSON file with validation and generating list with content
- Validation of login form on submit
- Communication with MongoDb database and simple php API
- Regexp checking on keyup
- Counting words in textarea and displain list below
- Reading batery state for navigator object
- Creation table of multiplication using pure javascript
- Inheritace of objects and tool for writing test for these objects
- Ajax communication with json-server that modify list of girs on refresh!
- Php that set up cookie session and allow to use it on client side
To fully open project you will need 4 terminals - one for any of servers. Before cloning repo create catalog for it. Enter tho catalog and setup any part of code below in separate terminal. You need to have free the following ports:
- 8000 - php-mongo
- 8040 - php-cookie
- 3000 - json-server
http-server web
sudo apt-get install php-mongodb
cd api && php -S localhost:8000 api.php
cd php && php -S localhost:8040
./node_modules/json-server/lib/cli/bin.js src/data/db.json
- live server in developing mode
- fully, flexible assets managing
- live nav-bar for single page app
- code encapsulation by anonymous functions
- Cross-Origin Resource Sharing
- npm
- gulp
- bower
- mongodb
- bootstrap
- jquery
- font-avesome
To start api server type:
cd api && php -S localhost:8000 api.php
Resources:
- words
Methods:
- GET
/words
- GET
/words/{id}
- POST
/words
- DELETE
/words/{id}
Description:
To get all words in database use:
http GET localhost:8000/words
Response:
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Host: localhost:8000
X-Powered-By: PHP/7.0.12-1+deb.sury.org~xenial+1
{
"words": [
{
"id": "581e489514c3cd5b41341038",
"word": "example"
},
{
"id": "581e4a668b04141faa42bce1",
"word": "word"
}
]
}
To get one word from database use:
http GET localhost:8000/words/581f0d1a8b04141cb54294b1
Response:
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Host: localhost:8000
X-Powered-By: PHP/7.0.12-1+deb.sury.org~xenial+1
{
"words": {
"id": "581f0d1a8b04141cb54294b1",
"word": "AAA"
}
}
To add one word to database use:
http POST localhost:8000/words word="new word"
Response:
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
Host: localhost:8000
X-Powered-By: PHP/7.0.12-1+deb.sury.org~xenial+1
{
"words": {
"id": "581f19a98b04141cb54294bc",
"word": "new word"
}
}
To delete word from database use:
http DELETE localhost:8000/words/581e489514c3cd5b41341038
Response:
HTTP/1.1 202 Accepted
Connection: close
Content-Type: application/json
Host: localhost:8000
X-Powered-By: PHP/7.0.12-1+deb.sury.org~xenial+1
[]
Todo: Type test of api in Behat. Simplify setting up project. I would like to open 4 servers in one command.
Instalation of mongodb (ubuntu 16.04)
Instalation php + mongodb
sudo apt-get install php-mongodb
sudo apt-get install php7.0-dev
sudo apt install php-pear
sudo pecl install mongodb
Saving pass of git
git config --global credential.helper cache
Add repo with mongo
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Update packages list in system
sudo apt-get update
Install mongo db
sudo apt-get install -y mongodb-org
and driver of mongo db for PHP
sudo apt-get install php-mongodb
Start server
sudo service mongod start
You can diagnose state of server by:
service mongod status
mongo
ctrl+x
Next after clone repo from:
git clone https://github.com/gustawdaniel/javascript_training.git
do
cd javascript_training
open console 2 and start php servrer
cd api && php -S localhost:8000 api.php
in firs console
sudo apt install httpie
Strange js behaviors
x = new String("x")
typeof x
"object"