Skip to content

Instantly share code, notes, and snippets.

Nitish Phanse nitish24p

View GitHub Profile
View AsyncUser.js
exports.createOrAddUserAsync = async (req, res, next) => {
try {
const user = await User.findUserPromise(req.body);
if (user) {
const existingUser = new User(user);
const data = await existingUser.updatePromise(req.body);
return res.send(data);
}
const newUser = new User();
View UserPromise.js
exports.createOrAddUserPromise = (req, res, next) => {
User.findUserPromise(req.body)
.then(user => {
if (user) {
const existingUser = new User(user);
return newUser.updatePromise(req.body)
}
const newUser = new User();
return newUser.savePromise(req.body);
})
View UserCallback.js
exports.createOrAddUser = (req, res, next) => {
User.findUser(req.body, (err, data) => {
if (err) {
return next(err)
};
if (data) {
const existingUser = new User(data);
existingUser.update(req.body, (err, data) => {
if (err) {
View User.js
const user = {
id: 1,
username: 'Nitish',
age: 25,
profession: 'dev'
}
const error = new TypeError('Something broke in the db');
class User {
@nitish24p
nitish24p / index.js
Last active Jun 26, 2018
index.js for sever
View index.js
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const userController = require('./userController');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/user', userController.createOrAddUser);
@nitish24p
nitish24p / Server markup.js
Last active Apr 15, 2018
Markup for server side render
View Server markup.js
/* @flow */
import { StaticRouter } from 'react-router-dom';
import { getBundles } from 'react-loadable/webpack';
import App from './../App';
import Loadable from 'react-loadable';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import bundleStats from './../../dist/build/react-loadable.json';
@nitish24p
nitish24p / server.js
Created Jan 18, 2018
Code split react loadable
View server.js
/// Somewhere on your express server
import Loadable from 'react-loadable';
Loadable.preloadAll().then(() => {
server.listen(port, function () {
console.log('Express server listening on port ' + server.address().port);
});
});
View AsyncClientSplit.js
import * as RoutingConstants from './constants/RoutingConstants';
import { Route, Switch } from 'react-router-dom';
import LoadingComponent from './pages/LoadingComponent';
import React, { Component } from 'react';
function asyncComponent(importComponent: Function, LoadingComponent: Function) {
class AsyncComponent extends Component {
constructor(props) {
super(props);
@nitish24p
nitish24p / appClient.js
Last active Jan 18, 2018
Code splitting with Higher order Components
View appClient.js
import * as RoutingConstants from './constants/RoutingConstants';
import { Route, Switch } from 'react-router-dom';
import AboutUs from './pages/AboutUs';
import Products from './pages/Products';
import Homepage from './pages/Homepage';
import React, { Component } from 'react';;
class App extends Component<{}, {}> {
render() {
return (
View index.test.js
import test from 'ava';
const request = require('supertest');
const app = require('./../app.js');
test('check status', async t => {
const response = await request(app)
.get('/status');
t.is(response.status, 200);
t.deepEqual(response.body, {
You can’t perform that action at this time.