Skip to content

Instantly share code, notes, and snippets.

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');
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();
<form action="/process" method="POST">
<input type="hidden" name="_csrf" value="{{csrfToken}}">
<button type="submit">Submit</button>
</form>
async function email(address) {
try {
// Do something asynchronous that may throw...
await sendEmail({ to: address, from: 'noreply@domain.com', subject: 'Hello' });
} catch(err) {
if (err instanceof SomeCustomError) {
elegantlyHandleError(err)
} else {
throw err
}
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);
}
{
"name": "nodejs_auth",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon src/index.js",
"start": "node src/index.js"
},
"keywords": [],
const express = require('express')
const app = express();
const port = process.env.PORT || 3000
app.listen(port,() =>{
console.log('server is up on ' + port);
})
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');
});
const mongoose = require('mongoose')
const validator = require('validator')
const UserSchema = new mongoose.Schema({
name:{
type: String,
required: true,
trim: true
},
age:{
const mongoose = require('mongoose');
const PostSchema = new mongoose.Schema({
title:{
type:String,
unique:true,
required: true,
trim: true
},
description:{