-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gorki
authored and
gorki
committed
Nov 22, 2022
1 parent
eff0ce1
commit 22ad6e2
Showing
1 changed file
with
67 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,71 @@ | ||
const express = require('express') | ||
const app = express() | ||
const port = 3000 | ||
//express js | ||
var express = require('express'); | ||
var app = express(); | ||
var port = 3000; | ||
|
||
app.get('/', (req, res) => { | ||
res.send('Hello World!') | ||
}) | ||
//corse request | ||
var cors = require('cors') | ||
app.use(cors()) | ||
|
||
//enabling json body parse | ||
app.use(express.json()); | ||
|
||
//MONGOOSE - requiring library | ||
const mongoose = require('mongoose'); | ||
|
||
//auto increment sequence | ||
var AutoIncrement = require('mongoose-sequence')(mongoose); | ||
|
||
//MONGOOSE - connecting to database | ||
mongoose.connect('mongodb://localhost:27017/pastebin',function(err){ | ||
if(err){ | ||
console.log("error occured while connecting to database"); | ||
}else{ | ||
console.log("successfully connected to database"); | ||
} | ||
}); | ||
|
||
//users schema | ||
const UsersSchema = new mongoose.Schema({ | ||
first_name: String, | ||
last_name: String, | ||
account_type: String, | ||
email: { | ||
type: String, | ||
unique: true, | ||
index: true | ||
}, | ||
password: String | ||
},{ | ||
strict:false | ||
}); | ||
//users model | ||
const UsersModel = mongoose.model('users', UsersSchema,'users',{versionKey: false}); | ||
|
||
//users -> signup | ||
app.post("/signup",(json_data,res)=>{ | ||
//MONGOOSE - preparing json data for sending | ||
const new_signup = new UsersModel({first_name:json_data.body.first_name, | ||
last_name:json_data.body.last_name, | ||
account_type:json_data.body.account_type, | ||
email:json_data.body.email, | ||
password:json_data.body.password, | ||
}); | ||
|
||
//MONGOOSE - inserting data to database | ||
new_signup.save(function (err, data) { | ||
if(err){ | ||
if (err.code && err.code === 11000) { | ||
return res.json({status:3,msg:"Account Already Exists : "+json_data.body.email}); | ||
} | ||
return res.json({status:3,msg:"error occured while insertion "+err}); | ||
}else{ | ||
return res.json({status:1,msg:"account created successfully : "+json_data.body.email}); | ||
} | ||
}); | ||
}); | ||
|
||
//listing express | ||
app.listen(port, () => { | ||
console.log(`Example app listening on port ${port}`) | ||
console.log(`Example app listening on port ${port}`) | ||
}) |