Project is Live at : https://devport-app.herokuapp.com
const express = require('express') const app = express()app.get('/', function (req, res) { res.send('Hello World') }); app.listen(3000);
mongoose.connect('mongodb://localhost/my_database', { useNewUrlParser: true, useUnifiedTopology: trueSchema Definition
const Schema = mongoose.Schema; const ObjectId = Schema.ObjectId;const BlogPost = new Schema({ author: ObjectId, title: String, body: String, date: Date }); });
var bcrypt = require('bcryptjs'); bcrypt.genSalt(10, function(err, salt) { bcrypt.hash("B4c0/\/", salt, function(err, hash) { // Store hash in your password DB. }); });// Load hash from your password DB. bcrypt.compare("B4c0//", hash, function(err, res) { // res === true });
jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256' }, function(err, token) { console.log(token); }); // verify a token symmetric jwt.verify(token, 'shhhhh', function(err, decoded) { console.log(decoded.foo) // bar });
gravatar.url(email); gravatar.url(email, options); gravatar.url(email, options, protocol);
app.post('/user', [ // username must be an email check('username').isEmail(), // password must be at least 5 chars long check('password').isLength({ min: 5 }) ], (req, res) => { // Finds the validation errors in this request and wraps them in an object with handy functions const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(422).json({ errors: errors.array() }); }
npm install concurrently --save In package.json, escape quotes: "start": "concurrently \"command1 arg\" \"command2 arg\""
To run the backend server automatically when any changes are done.
npx create-react-app my-app cd my-app npm startdirect push
npm run build
heroku
//package.json scripts: { "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client" } //server mods if(process.env.NODE_ENV === 'production'){ //set static path app.use(express.static('client/build')); app.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html')); }); }