mirror of
https://github.com/anatolykopyl/registration.git
synced 2026-03-26 04:45:25 +00:00
Initial commit
Backend endpoints for getting all user data and adding a user
This commit is contained in:
4200
package-lock.json
generated
Normal file
4200
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
27
package.json
Normal file
27
package.json
Normal 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
40
server.js
Normal 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)
|
||||
Reference in New Issue
Block a user