commit 30d9237a7acf43694b4334c8f041892cc489e7de Author: Yik Teng Hie Date: Wed Jan 6 15:34:42 2021 +0800 How-to : express JS diff --git a/README.md b/README.md new file mode 100644 index 0000000..f4e3425 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# How To + +## Prerequisites + +* [VS Code](https://code.visualstudio.com/) +* [IntelliJ IDEA Community](https://www.jetbrains.com/idea/download/#section=windows) + +* [nodeJS 12.x](https://nodejs.org/en/download/releases/) +* [JDK 8](https://adoptopenjdk.net/) - For Spring Boot only +* [Postman](https://www.postman.com/) + +## Framework + +* Backend + * [expressJS](./backend/expressJS/README.md) + * springboot +* Frontend + * [reactJS](./frontend/reactJS/README.md) + * vueJS \ No newline at end of file diff --git a/backend/expressJS/README.md b/backend/expressJS/README.md new file mode 100644 index 0000000..e2bbabb --- /dev/null +++ b/backend/expressJS/README.md @@ -0,0 +1,117 @@ +# Express JS + +[Home](../../README.md) + +[Reference](https://expressjs.com/en/starter) + + + +Create a project skeleton + +Option1 : Using basic `npm` step + +```sh +$ mkdir myapp +$ cd myapp + +$ npm init + +$ npm install express --save + +``` + + + +Option2 : Using `npx` express application generator. + +```sh +$ mkdir myapp +$ cd myappnpm +$ npx express-generator +$ npm install +``` + + + +Launch `vscode` IDE + +```sh +$ code . +``` + + + +Validate the `./bin/www.js`. By default, server serve at port 3000 + +```javascript + +var port = normalizePort(process.env.PORT || '3000'); +app.set('port', port); + +``` + + + +Add new route in `app.js` for *helloworld* routing + +```javascript +var helloworldRouter = require('./routes/helloworld'); +//... +app.use('/', indexRouter); +app.use('/users', usersRouter); +// new routing to helloworld +app.use('/helloworld', helloworldRouter); +``` + + + +Create new routing for `helloworld.js` + +```javascript +var express = require('express'); +var router = express.Router(); + +// GET method +router.get('/', function (req, res) { + res.send('Hello World!') + }) + +// POST method +router.post('/', function (req, res) { + res.send('Got a POST request') +}) + +// PUT method +router.put('/user', function (req, res) { + res.send('Got a PUT request at helloworld/user') +}) + +module.exports = router; +``` + + + +Run the server + +```sh +$ npm start +... +> expressjs@0.0.0 start C:\dev\playground\expressJS +> node ./bin/www +``` + + + +Browse [localhost](http://localhost:3000). You should see the text in browser + +```text +Express +Welcome to Express +``` + + + +Use *Postman* to execute 'POST', 'PUT' methods + +[json](./postman_collection.json) + diff --git a/backend/expressJS/postman_collection.json b/backend/expressJS/postman_collection.json new file mode 100644 index 0000000..5cfd579 --- /dev/null +++ b/backend/expressJS/postman_collection.json @@ -0,0 +1,134 @@ +{ + "info": { + "_postman_id": "3d0d7bdd-fd8f-4697-8f82-1b046a77464a", + "name": "expressJS", + "description": "sample expressJS endpoint", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "POST helloworld", + "request": { + "method": "POST", + "header": [ + { + "key": "Authorization", + "value": "SENSETIMESTUDIO authorization token. Start from BASIC", + "type": "text" + } + ], + "body": { + "mode": "formdata", + "formdata": [ + { + "key": "firstContext", + "value": "\"{\\\"type\\\":\\\"image\\\",\\\"metadata\\\":{\\\"access\\\":\\\"base64\\\",\\\"info\\\":{\\\"data\\\":\\\"YOUR BASE64 IMAGE\\\"}}}\"", + "type": "text" + }, + { + "key": "secondContext", + "value": "\"{\\\"type\\\":\\\"image\\\",\\\"metadata\\\":{\\\"access\\\":\\\"base64\\\",\\\"info\\\":{\\\"data\\\":\\\"YOUR BASE64 IMAGE\\\"}}}\"", + "type": "text" + } + ] + }, + "url": { + "raw": "http://localhost:3000/helloworld", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "helloworld" + ] + } + }, + "response": [] + }, + { + "name": "GET helloworld", + "protocolProfileBehavior": { + "disableBodyPruning": true + }, + "request": { + "method": "GET", + "header": [ + { + "key": "Authorization", + "value": "SENSETIMESTUDIO authorization token. Start from BASIC", + "type": "text" + } + ], + "body": { + "mode": "formdata", + "formdata": [ + { + "key": "firstContext", + "value": "\"{\\\"type\\\":\\\"image\\\",\\\"metadata\\\":{\\\"access\\\":\\\"base64\\\",\\\"info\\\":{\\\"data\\\":\\\"YOUR BASE64 IMAGE\\\"}}}\"", + "type": "text" + }, + { + "key": "secondContext", + "value": "\"{\\\"type\\\":\\\"image\\\",\\\"metadata\\\":{\\\"access\\\":\\\"base64\\\",\\\"info\\\":{\\\"data\\\":\\\"YOUR BASE64 IMAGE\\\"}}}\"", + "type": "text" + } + ] + }, + "url": { + "raw": "http://localhost:3000/helloworld", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "helloworld" + ] + } + }, + "response": [] + }, + { + "name": "PUT helloworld", + "request": { + "method": "PUT", + "header": [ + { + "key": "Authorization", + "value": "SENSETIMESTUDIO authorization token. Start from BASIC", + "type": "text" + } + ], + "body": { + "mode": "formdata", + "formdata": [ + { + "key": "firstContext", + "value": "\"{\\\"type\\\":\\\"image\\\",\\\"metadata\\\":{\\\"access\\\":\\\"base64\\\",\\\"info\\\":{\\\"data\\\":\\\"YOUR BASE64 IMAGE\\\"}}}\"", + "type": "text" + }, + { + "key": "secondContext", + "value": "\"{\\\"type\\\":\\\"image\\\",\\\"metadata\\\":{\\\"access\\\":\\\"base64\\\",\\\"info\\\":{\\\"data\\\":\\\"YOUR BASE64 IMAGE\\\"}}}\"", + "type": "text" + } + ] + }, + "url": { + "raw": "http://localjost:3000/helloworld/user", + "protocol": "http", + "host": [ + "localjost" + ], + "port": "3000", + "path": [ + "helloworld", + "user" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/frontend/reactJS/README.md b/frontend/reactJS/README.md new file mode 100644 index 0000000..25b8dab --- /dev/null +++ b/frontend/reactJS/README.md @@ -0,0 +1,11 @@ +# React JS + +Bootstrapping a project + +```shell +npm install + +npx create-react-app + +``` +