Basic API Server using Node.js Express Body-Parser
Langkah-langkah:
1. Install Node.js (https://nodejs.org/en/download/)
2. #mkdir my-project && cd my-project
3. #npm init
4. #npm install express --save
5. #npm install body-parser --save
Berikut contoh code nya dengan contoh nama file server.js
var express = require("express");
var myParser = require("body-parser");
var app = express();
app.use(myParser.urlencoded({extended : true}));
app.use(myParser.json());
app.post("/register", function(request, response) {
console.log(request.body.username);
console.log(request.body.email);
response.send("OK");
});
app.listen(3000);
6. Menjalankan code diatas dengan #node server.js
7. Contoh mengirim data dengan format JSON dengan perintah curl
#curl -H "Content-Type: application/json" -XOST -d '{"username":"nasohi","email":"nasohi@ciptandani.com"}' http://107.102.182.127:3000/register
Referensi:
https://flaviocopes.com/node-request-data/
https://www.kompulsa.com/how-to-accept-and-parse-post-requests-in-node-js/
https://tecadmin.net/post-json-data-with-curl-command/
1. Install Node.js (https://nodejs.org/en/download/)
2. #mkdir my-project && cd my-project
3. #npm init
4. #npm install express --save
5. #npm install body-parser --save
Berikut contoh code nya dengan contoh nama file server.js
var express = require("express");
var myParser = require("body-parser");
var app = express();
app.use(myParser.urlencoded({extended : true}));
app.use(myParser.json());
app.post("/register", function(request, response) {
console.log(request.body.username);
console.log(request.body.email);
response.send("OK");
});
app.listen(3000);
6. Menjalankan code diatas dengan #node server.js
7. Contoh mengirim data dengan format JSON dengan perintah curl
#curl -H "Content-Type: application/json" -XOST -d '{"username":"nasohi","email":"nasohi@ciptandani.com"}' http://107.102.182.127:3000/register
https://flaviocopes.com/node-request-data/
https://www.kompulsa.com/how-to-accept-and-parse-post-requests-in-node-js/
https://tecadmin.net/post-json-data-with-curl-command/
Komentar