
mongodb://<dbuser>:<dbpassword>@<host>:<port>/<dbname> mongodb://admin:superSecretPassword@ds111885.mlab.com:11885/medium // init project const express = require('express'); // the library we will use to handle requests const app = express(); // instantiate express app.use(require("cors")()) // allow Cross-domain requests app.use(require('body-parser').json()) // automatically parses request data to JSON // base route app.get("/", function (request, response) { response.send("TODO") // always responds with the string "TODO" }); // base route app.post("/", function (request, response) { response.send("TODO") // always responds with the string "TODO" }); app.put("/", function (request, response) { response.send("TODO") // always responds with the string "TODO" }); // listen for requests, the process.env.PORT is needed because // we are using glitch, otherwise you could have written 80 or whatever var listener = app.listen(process.env.PORT, function () { console.log('Your app is listening on port ' + listener.address().port); }); express - this is the library that we will use to process requests to the server.use(require(cors)) . These are requests from a website that is hosted in one domain, to a server in another domain.app.use(require('body-parser').json()) command automatically parses JSON requests for us.get route that we want to process, and the processing callback function itself. When someone opens the page / of our site, this request will be processed by a callback. The base domain will be relative, so if you have the address of the website shiny-koala.glitch.com , then the route /about will turn into shiny-koala.glitch.com/about .GET method on your server. HTTP methods are only types of requests that you can address to the server. We will only use these:GET - this method is used to extract resources from the server. For example, if you open Facebook, then all the necessary HTML, CSS and JavaScript are downloaded.POST - this method is used to create resources on the server. For example, when you write something on Facebook, what you write is sent to the social network servers in a POST request.PUT - this method is used to update resources on the server. For example, when you edit your post, all your edits are sent to the Facebook server in a PUT request.app.post and app.put work the same way as app.get , they only process POST and PUT methods instead of GET./ZaninAndrea or /JohnGreen ./:user/ZaninAndrea will be caught./Johnny45 will be caught./alex/score will not be caught.user value of the variable request.params.user // base route app.get("/:user", function (request, response) { response.send(request.params.user) }); // base route app.post("/:user", function (request, response) { response.send(request.params.user) }); // base route app.put("/:user", function (request, response) { response.send(request.params.user) }); mongodb library. It can be installed in two ways: npm install mongodb --save package.json file and click on the Add package button. const mongodb = require('mongodb'); // load mongodb const uri = process.env.URI; .env file that is invisible to others. URI=mongodb://admin:PASSWORD@ds111885.mlab.com:11885/medium .env file into the process.env variable. mongodb.MongoClient.connect(uri, function(err, db) { // base route app.get("/:user", function (request, response) { response.send(request.params.user) }); // base route app.post("/:user", function (request, response) { response.send(request.params.user) }); // base route app.put("/:user", function (request, response) { response.send(request.params.user) }); // listen for requests, the process.env.PORT is needed because // we are using glitch, otherwise you could have written 80 or whatever var listener = app.listen(process.env.PORT, function () { console.log('Your app is listening on port ' + listener.address().port); }); }) user collection (it will be created when it is first accessed). mongodb.MongoClient.connect(uri, function(err, db) { const collection = db.collection('users') // ... } POST path. We will use it when we first add user data. Then we use the PUT method to update the data. app.post("/:user", function (request, response) { // inserts a new document on the server collection.insertOne({ ...request.body, user : request.params.user }, function (err, r) { if (err){ response.send("An error occured") }else{ response.send("All well") } }) }); collection.insertOne method adds a new document to the collection. In our case, each user has his own document. { ...request.body, user : request.params.user } GET method. app.get("/:user", function (request, response) { collection.find({ user : request.params.user }).toArray(function (err, docs) { if (err){ response.send("An error occured") }else{ response.send(docs) } }) }); PUT method. // base route app.put("/:user", function (request, response) { collection.updateOne({ user : request.params.user }, {$set:{ ...request.body, user : request.params.user }}, function (err, r) { if (err){ response.send("An error occured") }else{ response.send("All well") } }) }); GET method.Source: https://habr.com/ru/post/343334/
All Articles