Initial commit

Backend endpoints for getting all user data and adding a user
This commit is contained in:
2021-03-22 16:08:31 +03:00
commit aadfd0708b
3 changed files with 4267 additions and 0 deletions

4200
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

27
package.json Normal file
View File

@@ -0,0 +1,27 @@
{
"name": "registration",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"dev": "nodemon server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/anatolykopyl/registration.git"
},
"author": "Anatoly Kopyl",
"license": "ISC",
"bugs": {
"url": "https://github.com/anatolykopyl/registration/issues"
},
"homepage": "https://github.com/anatolykopyl/registration#readme",
"dependencies": {
"bcrypt": "^5.0.1",
"express": "^4.17.1",
"mongodb": "^3.6.5"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}

40
server.js Normal file
View File

@@ -0,0 +1,40 @@
const express = require('express')
const app = express()
const {MongoClient} = require('mongodb')
const bcrypt = require('bcrypt')
app.use(express.json())
const uri = "mongodb://localhost?retryWrites=true&w=majority"
const client = new MongoClient(uri, { useUnifiedTopology: true })
app.get('/get-users', async (_, res) => {
try {
await client.connect()
const users = await client.db('reg_example').collection('users').find().toArray()
res.send(users)
} catch (e) {
console.log("Database error: " + e)
res.status(500)
} finally {
client.close()
}
})
app.get('/register', async (req, res) => {
const hashedPass = await bcrypt.hash(req.query.pass, 10)
try {
await client.connect()
await client.db('reg_example').collection('users').insertOne({
login: req.query.login,
pass: hashedPass
})
res.status(201).send();
} catch (e) {
console.log("Database error: " + e)
res.status(500)
} finally {
client.close()
}
})
app.listen(3000)