-
Notifications
You must be signed in to change notification settings - Fork 15
/
arch.sh
executable file
·951 lines (812 loc) · 21.8 KB
/
arch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
echo '******** lets name your project now ********'
echo -n "Enter your project name > "
read projectname
mkdir $projectname
cd $projectname
echo 'We are ready now so lets setup project
---------------------------------------------------'
npm init
echo 'lets add git to it'
git init
echo 'Creating .gitignore'
touch .gitignore
echo "
node_modules
npm-debug.log
/config.js
/*.pid
*.rdb
.DS_Store
" >> .gitignore
cp ../migration.babel.stub migration.babel.stub
echo 'Now let us install some dev dependencies
---------------------------------------------------'
npm install --save-dev [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
npm install --save-dev [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
echo 'and some more dependencies
---------------------------------------------------'
npm install --save bcrypt body-parser cors errorhandler express lodash moment rand-token request-promise tabel pg
echo '******** Dependencies installed successfully *********'
echo 'creating .babelrc'
touch .babelrc
echo '
{
"presets": ["stage-0", "es2015"],
"plugins": [
"add-module-exports"
]
}
' >> .babelrc
echo 'Creating .eslintrc'
touch .eslintrc
echo '
{
"ecmaFeatures": {
"arrowFunctions": true,
"binaryLiterals": false,
"blockBindings": true,
"classes": true,
"defaultParams": true,
"destructuring": true,
"forOf": true,
"generators": true,
"modules": true,
"objectLiteralComputedProperties": true,
"objectLiteralDuplicateProperties": false,
"objectLiteralShorthandMethods": true,
"objectLiteralShorthandProperties": true,
"octalLiterals": false,
"regexUFlag": false,
"regexYFlag": false,
"restParams": true,
"spread": true,
"superInFunctions": true,
"templateStrings": true,
"unicodePointEscapes": true,
"globalReturn": false,
"jsx": true
},
"env": {
"browser": true,
"node": true,
"mocha": true
},
"parser": "babel-eslint",
"plugins": [
"babel",
"react"
],
"rules": {
/*Possible Errors */
"comma-dangle": [1, "never"],
"no-cond-assign": [1, "except-parens"],
"no-console": 0,
"no-constant-condition": 1,
"no-control-regex": 1,
"no-debugger": 1,
"no-dupe-args": 1,
"no-dupe-keys": 1,
"no-duplicate-case": 0,
"no-empty-character-class": 1,
"no-empty": 1,
"no-ex-assign": 1,
"no-extra-boolean-cast": 1,
"no-extra-parens": 0,
"no-extra-semi": 1,
"no-func-assign": 1,
"no-inner-declarations": [1, "functions"],
"no-invalid-regexp": 1,
"no-irregular-whitespace": 1,
"no-negated-in-lhs": 1,
"no-obj-calls": 1,
"no-regex-spaces": 1,
"no-reserved-keys": 0,
"no-sparse-arrays": 1,
"no-unexpected-multiline": 1,
"no-unreachable": 1,
"use-isnan": 1,
"valid-jsdoc": 1,
"valid-typeof": 1,
/* Best Practices */
"accessor-pairs": 0,
"block-scoped-var": 0, // see Babel section
"complexity": 0,
"consistent-return": 1,
"curly": [1, "all"],
"default-case": 0,
"dot-notation": [1, { "allowKeywords": true, "allowPattern": "" }],
"dot-location": [1, "property"],
"eqeqeq": 1,
"guard-for-in": 0,
"no-alert": 1,
"no-caller": 1,
"no-console": 1,
"no-div-regex": 1,
//"no-else-return": 1,
"no-empty-label": 1,
"no-eq-null": 0,
"no-eval": 1,
"no-extend-native": 1,
"no-extra-bind": 1,
"no-fallthrough": 0,
"no-floating-decimal": 1,
"no-implied-eval": 1,
"no-iterator": 1,
"no-labels": 1,
"no-lone-blocks": 1,
"no-loop-func": 1,
"no-multi-spaces": 1,
"no-multi-str": 1,
"no-native-reassign": 1,
"no-new-func": 1,
"no-new-wrappers": 1,
"no-new": 1,
"no-octal-escape": 1,
"no-octal": 1,
"no-param-reassign": 0,
"no-process-env": 0,
"no-proto": 1,
"no-redeclare": 1,
"no-return-assign": 1,
"no-script-url": 1,
"no-self-compare": 1,
"no-sequences": 1,
"no-throw-literal": 1,
"no-unused-expressions": 0,
"no-void": 0,
"no-warning-comments": [1, { "terms": ["todo", "tofix"], "location": "start" }],
"no-with": 1,
"radix": 1,
"vars-on-top": 1,
"wrap-iife": [1, "inside"],
"yoda": [1, "never"],
/* Strict Mode */
"strict": [1, "never"],
/* Variables */
"no-catch-shadow": 0,
"no-delete-var": 1,
"no-label-var": 1,
"no-shadow-restricted-names": 1,
"no-shadow": 0,
"no-undef-init": 1,
"no-undef": 1,
"no-undefined": 1,
"no-unused-vars": [1, { "vars": "local", "args": "after-used" }],
"no-use-before-define": 0,
/* Node.js */
"handle-callback-err": 1,
"no-mixed-requires": 1,
"no-new-require": 1,
"no-path-concat": 1,
"no-process-exit": 1,
"no-restricted-modules": [1, ""], // add any unwanted Node.js core modules
"no-sync": 1,
/* Stylistic Issues */
"array-bracket-spacing": [1, "never"],
"brace-style": [1, "1tbs", { "allowSingleLine": true }],
//"camelcase": [1, { "properties": "never" }],
"comma-spacing": [1, { "before": false, "after": true }],
"comma-style": [1, "last"],
"computed-property-spacing": 0,
"consistent-this": 0,
"eol-last": 1,
"func-names": 1,
"func-style": 0,
"indent": [1, 2, {"SwitchCase": 1}],
"key-spacing": [1, { "beforeColon": false, "afterColon": true }],
"linebreak-style": 0,
"max-nested-callbacks": [0, 3],
"new-cap": 0, // see Babel section
"new-parens": 1,
"newline-after-var": 0,
"no-array-constructor": 1,
"no-continue": 1,
"no-inline-comments": 0,
"no-lonely-if": 0,
"no-mixed-spaces-and-tabs": 1,
"no-multiple-empty-lines": [1, { "max": 1 }],
"no-nested-ternary": 0,
"no-new-object": 1,
"no-spaced-func": 1,
"no-ternary": 0,
"no-trailing-spaces": 1,
"no-underscore-dangle": 0,
"no-unneeded-ternary": 1,
"object-curly-spacing": 0, // see Babel section
"one-var": [1, "never"],
"operator-assignment": [1, "never"],
"padded-blocks": [0, "never"],
"quote-props": [0, "as-needed"],
"quotes": [1, "single"],
"semi-spacing": [1, { "before": false, "after": true }],
"semi": [1, "always"],
"sort-vars": 0,
"space-after-keywords": 0,
"space-before-blocks": [1, "always"],
"space-before-function-paren": [1, "never"],
"space-in-parens": [1, "never"],
//"space-infix-ops": 1,
"space-return-throw-case": 1,
"space-unary-ops": 0,
"spaced-comment": [1, "always"],
"wrap-regex": 1,
/* ECMAScript 6 */
"constructor-super": 1,
"generator-star-spacing": 0, // see Babel section
"no-this-before-super": 1,
"no-var": 1,
"object-shorthand": 0, // see Babel section
"prefer-const": 1,
/* Legacy */
"max-depth": [0, 3],
"max-len": [1, 121, 2],
"max-params": 0,
"max-statements": 0,
"no-bitwise": 1,
"no-plusplus": 1,
/* Babel */
// "babel/block-scoped-var": 1,
"babel/object-shorthand": [1, "always"],
// "babel/generator-star": 1, // deprecated
"babel/generator-star-spacing": [1, "after"],
"babel/new-cap": 0,
"babel/object-curly-spacing": [1, "never"],
// "babel/space-in-brackets": 1, // deprecated
}
}
' >> .eslintrc
echo 'Creating config.js'
touch config.js
echo "
export default {
orm: {
db: {
client: 'postgresql',
connection: {
database: 'dev',
host: 'localhost',
port: 5432
},
pool: {
min: 2,
max: 10
},
migrations: 'knex_migrations'
},
redis: {
host: 'localhost',
port: '6379'
}
},
auth: {
lifetime: 2 * 24 * 3600 * 1000
},
http: {
host: '0.0.0.0',
port: 3000
}
};
" >> config.js
mkdir app
cd app
echo '********** Creating folders
1. http: where all your routes are present.
2. orm: where all your relationships and mapping with database occur.
3. reducer: which has a custom vaildator created.
4. tasks: for creating task triggered from cli
5. util: where all your utility files are saved.
**********'
mkdir auth http orm reducer tasks util
cd util
touch index.js
echo "
import {
isObject,
isArray,
isFunction,
isRegExp,
isNumber,
isString,
isElement,
isDate
} from 'lodash';
export function handleAsyncExceptions() {
if (handleAsyncExceptions.hooked === false) {
process.on('unhandledRejection', (err) => {
throw err;
});
handleAsyncExceptions.hooked = true;
}
}
export function isEmail(email) {
const re = /\S+@\S+\.\S+/;
return re.test(email);
}
handleAsyncExceptions.hooked = false;
export function printIp() {
const os = require('os');
const ifaces = os.networkInterfaces();
Object.keys(ifaces).forEach((ifname) => {
let alias = 0;
ifaces[ifname].forEach((iface) => {
if (iface.family !== 'IPv4' || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
console.log(ifname + ':' + alias, iface.address);
} else {
// this interface has only one ipv4 adress
console.log(ifname, iface.address);
}
alias = alias+1;
});
});
}
export function isUsableObject(val) {
return isObject(val) && ! (
isArray(val) || isFunction(val) || isRegExp(val) || isNumber(val) || isString(val) ||
isElement(val) || isDate(val)
);
}
" >> index.js
echo '********** Util folder setup completed **********'
cd ../tasks
mkdir seeds
touch index.js seed.js dev.js
echo "
import dev from './dev';
import migrate from './migrate';
import seed from './seed';
export default {
dev,
migrate,
seed
};
" >> index.js
cp ../../../migrate.js migrate.js
echo "
import seeds from './seeds';
export default async function run() {
await Object.keys(seeds).map((tableName) => seeds[tableName]).reduce(
(chain, seed) => chain.then(() => seed.run()),
Promise.resolve({})
)
;
}
" >> seed.js
echo "
export default async function run() {
console.log('dev');
}
" >> dev.js
cd seeds
touch index.js
echo "
export default {
};
" >> index.js
echo '********** tasks setup successfully **********'
cd ../../reducer
touch index.js Validator.js
echo "
import Validator from './Validator';
export {
Validator
};
" >> index.js
echo "
import {assign, isArray, toPlainObject} from 'lodash';
import {isUsableObject} from 'app/util';
export default class Validator {
constructor(validations=[]) {
this.validations = new Map();
this.addValidations(validations);
}
addValidations(validations=[]) {
if (isUsableObject(validations)) {
validations = toPlainObject(validations);
validations = Object.keys(validations).map((k) => ({key: k, validation: validations[k]}));
}
validations.forEach(({key, validation}) => {
this.validations.set(key, validation);
});
return this;
}
addValidation({key, validation}) {
this.validations.set(key, validation);
return this;
}
merge(validator) {
Array.from(validator.validation.keys()).forEach((k) => {
this.validations.set(k, validator.validations.get(k));
});
return this;
}
errors(input={}) {
const keys = Array.from(this.validations.keys());
return Promise.all(
keys.map((k) => {
const validated = this.validations.get(k).bind(this)(input[k], input, k);
if (validated instanceof Promise) {
return validated.then((errors) => {
return {key: k, errors};
});
} else if (validated instanceof Validator) {
return validated.errors(input[k]).then((errors) => {
return {key: k, errors};
});
} else {
return {key: k, errors: validated};
}
})
)
.then((errorMessages) => errorMessages.filter((msg) => {
return isArray(msg.errors) ? msg.errors.length > 0 : !!msg.errors;
}))
.then((errorMessages) => {
if (errorMessages.length === 0) {
return null;
} else {
return errorMessages.reduce((allErrors, {key, errors}) => {
return assign(allErrors, {
[key]: isArray(errors) ? errors : (
isUsableObject(errors) ? toPlainObject(errors) : [errors]
)
});
}, {});
}
});
}
run(input={}) {
return this.errors(input).then((errors) => {
if (errors) {
throw errors;
} else {
return input;
}
});
}
}
" >> Validator.js
cd ../orm
mkdir tables
touch config.js index.js
echo "
import {orm} from 'config';
export default orm;
" >> config.js
echo "
import Tabel from 'tabel';
import config from './config';
import loadTables from './tables';
const orm = new Tabel(config);
loadTables(orm);
export default orm.exports;
" >> index.js
cd tables
touch index.js
echo "
import users from './users';
export default function loadTables(orm) {
users(orm);
}
" >> index.js
touch users.js
echo "
export default function loadTables(orm) {
orm.defineTable({
name: 'users',
props: {
autoId: true,
timestamps: true
}
})
}
" >> users.js
echo "Orm setup successfully"
cd ../../http
mkdir auth
touch config.js index.js
echo "
import {http} from 'config';
export default http;
" >> config.js
echo "
import express from 'express';
import bodyParser from 'body-parser';
import errorhandler from 'errorhandler';
import cors from 'cors';
import {loggedIn} from 'app/auth/filters';
import auth from './auth';
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: true}));
// parse application/json
app.use(bodyParser.json({limit: '50mb'}));
// parse multipart-form-data
app.use(cors());
// handle errors, should only be enabled in dev, but everything is dev
// right now
app.use(errorhandler());
if (process.env.NODE_ENV === 'production') {
// trust proxy in production from local nginx front server
app.set('trust proxy', 'loopback');
}
app.use('/auth', auth);
app.get('/', (req, res) => {
res.send({msg: 'Nodejs is live. Lets go and chanage the world.'});
});
app.use(loggedIn);
// catch all route
app.all('*', (req, res) => {
res.status(404).send({msg: 'not found'});
});
export default app;
" >> index.js
cd auth
touch index.js
echo "
import express from 'express';
import {isString, omit, isUndefined} from 'lodash';
import bcrypt from 'bcrypt';
import {table} from 'app/orm';
import {login, logout} from 'app/auth';
import {Validator} from 'app/reducer';
import {isEmail} from 'app/util';
import {
loggedIn,
loggedOut,
getAuthKeyFromRequest
} from 'app/auth/filters';
const app = express();
app.post('/login', loggedOut, async (req, res) => {
const {email, password} = req.body;
const {success, token, user} = await login(email, password);
if (success) {
const teams = await table('users')
.where({id: user.id})
.first()
;
return res.status(200).send({msg: 'logged in', token, user: omit(user, ['password']), teams});
} else {
return res.status(400).send({email: 'Email password combination is invalid.'});
}
});
app.post('/logout', loggedIn, async (req, res) => {
const authKey = getAuthKeyFromRequest(req);
await logout(authKey);
return res.send({msg: 'logged out'});
});
async function alreadySignedUp(req, res, next) {
if (!req.body.email || req.body.email.length === 0) {
return next();
}
const existing = await table('users')
.where({email: req.body.email})
.all()
;
if(existing.length > 0) {
return res.status(409).send({email: ['Email already taken.']});
} else {
return next();
}
}
const signUpValidator = new Validator({
['email'](email) {
return (!isUndefined(email) && isEmail(email) && isString(email)) ? null : 'Email is invalid.';
},
['name'](name) {
return (!isUndefined(name) && name.length > 0 && isString(name)) ? null : 'Name is invalid.';
},
['password'](password) {
if(!isUndefined(password) && isString(password) && password.length > 4) {
return null;
} else {
return 'Password should be atleast 5 characters long.';
}
}
});
app.post('/signup', alreadySignedUp, async (req, res) => {
const errors = await signUpValidator.errors(req.body);
if (errors) {
return res.status(400).send(errors);
}
const {email, password, name} = req.body;
const user = await table('users').insert({
email: email.trim(),
password: await new Promise((resolve) => bcrypt.hash(password, 10, (_, hash) => resolve(hash))),
name: name.trim()
});
if (user) {
return res.status(200).send({msg: 'Signup success.', user: omit(user, ['password'])});
} else {
return res.status(400).send({msg: 'Signup failed.'});
}
});
export default app;
" >> index.js
echo "********** Http setup successfully **********"
cd ../../auth
touch config.js filters.js index.js
echo "
import {auth} from 'config';
export default auth;
" >> config.js
echo "
import {check} from './index';
export async function loggedIn(req, res, next) {
const authKey = getAuthKeyFromRequest(req);
const user = await check(authKey);
if (user !== null) {
req.user = user;
req.authKey = authKey;
next();
} else {
res.status(401).send({msg: 'Unauthorized'});
}
}
export async function loggedOut(req, res, next) {
const authKey = getAuthKeyFromRequest(req);
const user = await check(authKey);
if (user !== null) {
res.status(400).send({msg: 'already logged in'});
} else {
next();
}
}
export function getAuthKeyFromRequest(req) {
return req.header('auth-token');
}
" >> filters.js
echo "
import {isString, shuffle} from 'lodash';
import bcrypt from 'bcrypt';
import randToken from 'rand-token';
import {table, cache} from 'app/orm';
import {isUsableObject} from 'app/util';
import config from './config';
const authHash = cache.hash('auth');
export async function login(email, password) {
const user = await findUserByCredentials(email, password);
if (user === null) {
return {success: false, token: null, user: null};
}
const authKey = await generateUniqueAuthKey(user);
await authHash.set(authKey, user.id, config.lifetime);
return {success: true, token: authKey, user};
}
export async function check(authKey) {
const userId = await authHash.get(authKey);
if (userId === null) {
return null;
}
const user = await table('users').find({id: userId});
return isUsableObject(user) ? user : null;
}
export async function logout(authKey) {
await authHash.del(authKey);
return true;
}
async function findUserByCredentials(email, password) {
if (! isString(email) || ! isString(password)) {
return null;
}
const user = await table('users').find({email});
if (! isUsableObject(user)) {
return null;
}
const isValidUser = await bcryptCheck(password, user.password);
return isValidUser ? user : null;
}
async function bcryptCheck(password, hash) {
return await new Promise((resolve) => (
bcrypt.compare(password, hash, (err, res) => {
if (err) {
return resolve(false);
}
return resolve(res);
})
));
}
async function generateUniqueAuthKey(user) {
const key = shuffle(randToken.generate(72)).join('');
const existing = await authHash.get(key);
if (existing === null) {
return key;
} else {
return await generateUniqueAuthKey(user);
}
}
" >> index.js
echo "Auth setup successfully"
cd ../../app
touch cli.js server.js
echo "
import {orm} from 'app/orm';
import {handleAsyncExceptions} from 'app/util';
import tasks from './tasks';
// the task runner
async function run(taskName, ...args) {
const taskNames = Object.keys(tasks);
if (taskNames.indexOf(taskName) === -1) {
console.log('Tasks are', taskNames)
return Promise.resolve(null);
}
try {
return await tasks[taskName](...args);
} catch (err) {
throw err;
}
}
if (require.main === module) {
// handle async exceptions
handleAsyncExceptions();
// run the task runner
const [taskName, ...args] = process.argv.slice(2);
(async () => {
await run(taskName, ...args);
await new Promise((resolve) => setTimeout(async () => {
await orm.close();
resolve();
}, 2500));
})();
}
export default run;
" >> cli.js
echo "
import http from 'app/http';
import config from 'app/http/config';
import {printIp, handleAsyncExceptions} from 'app/util';
async function run() {
http.listen(config.port, config.host, () => {
console.log('server is running ......')
printIp();
});
}
export default run;
if (require.main === module) {
handleAsyncExceptions();
run(...process.argv.slice(2));
}
" >> server.js
echo 'Copy paste this in package.json remove the old scripts
"scripts": {
"task": "NODE_PATH=. babel-node ./app/cli.js",
"server": "NODE_PATH=. babel-node ./app/server.js",
"nodemon:server": "NODE_PATH=. nodemon --exec \"babel-node\" ./app/server.js"
},
"babel": {
"presets": [
"es2015"
]
},'
echo -n "****** Press enter when you have done that"
read ok
echo ">>>>Go and create database dev and then open terminal and reach to project directory and type : npm run task migrate make CreateUsersTable <<<<"
echo -n "****** Press enter when you have done that"
read ok
echo "A migration directory at root of project is created copy paste or edit as required
function up(knex, Promise) {
return knex.schema.createTable('users', (t) => {
t.uuid('id').primary();
t.string('email').unique();
t.string('name');
t.date('birthday');
t.string('gender');
t.string('phone_number');
t.string('password');
t.timestamps();
});
}
function down(knex, Promise) {
return knex.schema.dropTable('users');
}
module.exports = {up, down};
"
echo -n "****** Press enter when you have done that"
read ok
echo ">>>> Open the terminal again and reach to the project and then do : npm run task migrate latest <<<<<"
echo "**************** successfully setup *****************"
echo "Go inside project and type npm run nodemon:server once started Lets go to browser and type localhost:3000"