just-jormungandr / projects

🌐 Projects
https://gitlab.com/impactbyte/learn/course-fullstackweb?nav_source=navbar
The Unlicense
2 stars 1 forks source link

Express-MongoDB #21

Open haydanu opened 4 years ago

haydanu commented 4 years ago
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");
const objectId = require("mongodb").ObjectID;

const app = express();
const port = process.env.PORT || 3000;
const url = "mongodb://localhost:27017";
const dbName = "project-1";
let db;

app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

MongoClient.connect(
  url,
  { useNewUrlParser: true, useUnifiedTopology: true },
  function(err, client) {
    assert.equal(null, err);
    console.log("Connected successfully to server");

    db = client.db(dbName);
  }
);

app.get("/", (req, res) => {
  db.collection("users")
    .find()
    .toArray((err, result) => {
      try {
        res.send(result);
      } catch (error) {
        console.log(err);
        console.log(error);
      }
    });
});

app.delete("/:id", (req, res) => {
  db.collection("users").deleteOne(
    {
      _id: objectId(req.params.id)
    },
    (err, result) => {
      try {
        res.send(result);
      } catch (error) {
        console.log(err);
        console.log(error);
      }
    }
  );
});

app.put("/:id", (req, res) => {
  db.collection("users").updateOne(
    {
      _id: { $eq: objectId(req.params.id) }
    },
    {
      $set: { name: "nama yang terupdate", hallo: "iya" }
    },
    (err, result) => {
      try {
        res.send(result);
      } catch (error) {
        console.log(err);
        console.log(error);
      }
    }
  );
});

app.post("/", (req, res) => {
  db.collection("users").updateOne(
    {
      name: "Ian Kasela",
      umur: 20,
      role: "user",
      address: "kemang",
      hobbies: req.body.hobbies
    },
    (err, result) => {
      try {
        res.send(result);
      } catch (error) {
        console.log(error);
        console.log(err);
      }
    }
  );
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
haydanu commented 4 years ago

@thatguyarsya @thomasfebrianseiei

haydanu commented 4 years ago

README.md example


# User Profile REST-API

## How To

### Run Your Teminal

* Clone this repository
```sh
git clone https://github.com/herdanuyp/user-profile.git
npm install

to get all user profile


```sh
http://localhost:3000/user/signup

to create new user

{
 "name": "your name",
 "phoneNumber": "your phone number",
 "email": "your email",
 "password": "your password"
}
http://localhost:3000/user/:userId/change

change your password

{
 "password": "your new password"
}
http://localhost:3000/user/:userId

to delete user
http://localhost:3000/user/:userId

to update user profile

{
 "name": "your name"
 "phoneNumber": "your phone number"
}
http://localhost:3000/user/login

to login

{
 "email": "your email"
 "password": "your new password"
}

API Endpoints

HTTP Routes Description
GET / ...
GET /user get all user profile .
POST /user/signup to sign up as a new user
POST /user/login to login .
DELETE /user/:userId . delete user
POST /user/:userId/change to change password
PUT /user/:userId . to update user profile
thatguyarsya commented 4 years ago

github repo

idhoramgg commented 4 years ago

higtub oper

jantoandriano commented 4 years ago

https://github.com/jantoandriano/mongo-express

afriansyahian commented 4 years ago

mongoDB-express

gunturbudi16 commented 4 years ago

github