This file contains hidden or 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
| var cors = require('cors'); | |
| var app = express(); | |
| app.use(cors()); | |
| app.use(function(req, res, next) { | |
| res.header("Access-Control-Allow-Origin", "*"); | |
| res.header('Access-Control-Allow-Methods', 'DELETE, PUT, GET, POST'); |
This file contains hidden or 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
| const cookieParser = require('cookie-parser'); | |
| const csrf = require('csurf'); | |
| const bodyParser = require('body-parser'); | |
| const express = require('express'); | |
| const csrfProtection = csrf({ cookie: true }); | |
| const parseForm = bodyParser.urlencoded({ extended: false }); | |
| const app = express(); |
This file contains hidden or 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
| <form action="/process" method="POST"> | |
| <input type="hidden" name="_csrf" value="{{csrfToken}}"> | |
| <button type="submit">Submit</button> | |
| </form> |
This file contains hidden or 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
| async function email(address) { | |
| try { | |
| // Do something asynchronous that may throw... | |
| await sendEmail({ to: address, from: '[email protected]', subject: 'Hello' }); | |
| } catch(err) { | |
| if (err instanceof SomeCustomError) { | |
| elegantlyHandleError(err) | |
| } else { | |
| throw err | |
| } |
This file contains hidden or 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
| const myfunc = async () =>{ | |
| const password = 'Red12345' | |
| const hashedPassword = await bcrypt.hash(password, 8) | |
| console.log(password); | |
| console.log(hashedPassword); | |
| //if a give password amtch the hash passwrod | |
| const isMatch = await bcrypt.compare('Red12345', hashedPassword) | |
| console.log(isMatch); | |
| } |
This file contains hidden or 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
| { | |
| "name": "nodejs_auth", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "dev": "nodemon src/index.js", | |
| "start": "node src/index.js" | |
| }, | |
| "keywords": [], |
This file contains hidden or 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
| const express = require('express') | |
| const app = express(); | |
| const port = process.env.PORT || 3000 | |
| app.listen(port,() =>{ | |
| console.log('server is up on ' + port); | |
| }) |
This file contains hidden or 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
| const mongoose = require('mongoose'); | |
| mongoose.connect('mongodb://<user>:<password>@ds127644.mlab.com:27644/nodeauth',{ | |
| useNewUrlParser: true, | |
| useCreateIndex: true | |
| }).then(() =>{ | |
| console.log('connected to database'); | |
| }).catch(() =>{ | |
| console.log('failed connected to database'); | |
| }); |
This file contains hidden or 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
| const mongoose = require('mongoose') | |
| const validator = require('validator') | |
| const UserSchema = new mongoose.Schema({ | |
| name:{ | |
| type: String, | |
| required: true, | |
| trim: true | |
| }, | |
| age:{ |
This file contains hidden or 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
| const mongoose = require('mongoose'); | |
| const PostSchema = new mongoose.Schema({ | |
| title:{ | |
| type:String, | |
| unique:true, | |
| required: true, | |
| trim: true | |
| }, | |
| description:{ |