Skip to content

Instantly share code, notes, and snippets.

View jasheloper's full-sized avatar

Jashele T. jasheloper

View GitHub Profile
@jasheloper
jasheloper / index.js
Last active November 8, 2019 00:29
Express server template
const server = require('./server.js');
server.listen(4000, () => {
console.log('\n*** Server Running on http://localhost:4000 ***\n');
});
@jasheloper
jasheloper / CRUD_server_sample.js
Last active November 4, 2019 22:38
Sample CRUD server; CREATE, READ, UPDATE, DESTORY
const express = require('express');
const port = 5000;
const server = express();
server.use(express.json())
server.get('/hobbits', (req, res) => {
res.send('Welcome to Hobbiton!');
@jasheloper
jasheloper / Router.js
Last active December 3, 2019 13:08
Creating a file to will handle all routes related to the user resource.
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.status(200).send('hello from the GET /users endpoint');
});
@jasheloper
jasheloper / Central_Router.js
Last active January 13, 2020 15:26
create a router by itself and call it in another file - express - backend
// inside /api/apiRoutes.js <- this can be place anywhere and called anything
const express = require('express');
// if the other routers are not nested inside /api then the paths would change
const userRoutes = require('./users/userRoutes');
const productRoutes = require('./products/productRoutes');
const clientRoutes = require('./clients/clientRoutes');
const router = express.Router(); // notice the Uppercase R
@jasheloper
jasheloper / with_array.js
Last active January 13, 2020 15:25
combining middleware - express - backend
server.use(express.json());
server.use(helmet());
server.use(morgan('dev'));
server.use(methodLogger);
// use -
const middleware = [express.json(), helmet(), morgan('dev'), methodLogger];
server.use(middleware);
@jasheloper
jasheloper / knex-foreign-key-setup.js
Created November 26, 2019 00:58
Foreign Key Setup - In Knex, foreign key restrictions don’t automatically work. Whenever using foreign keys in your schema, add the following code to your knexfile. This will prevent users from entering bad data into a foreign key column
development: {
client: 'sqlite3',
useNullAsDefault: true,
connection: {
filename: './data/database.db3',
},
// needed when using foreign keys
pool: {
afterCreate: (conn, done) => {
// runs after a connection is made to the sqlite engine
@jasheloper
jasheloper / migrations-creating-2-tables.js
Last active November 26, 2019 01:04
Migrations - Creating 2 tables in the up function
exports.up = function(knex, Promise) {
return knex.schema
.createTable('farms', tbl => {
tbl.increments();
tbl.string('farm_name', 128)
.notNullable();
})
// we can chain together createTable
.createTable('ranchers', tbl => {
tbl.increments();
@jasheloper
jasheloper / many-to-many-migration.js
Last active January 13, 2020 15:24
migration with foreign key sample
.createTable('farm_animals', tbl => {
tbl.integer('farm_id')
.unsigned()
.notNullable()
.references('id')
// this table must exist already
.inTable('farms')
tbl.integer('animal_id')
.unsigned()
.notNullable()
@jasheloper
jasheloper / knexmigration.js
Last active January 13, 2020 15:23
knex migration sample - creating tables
exports.up = function(knex) {
return (knex.schema
.createTable('zoos', tbl => {
tbl.increments();
tbl.string('zoo_name', 128)
.notNullable()
.unique();
tbl.string('address', 128)
.notNullable()
.unique();
@jasheloper
jasheloper / auth_router.js
Last active January 13, 2020 15:23
auth router sample - bcrypt
const express = require("express");
const helmet = require("helmet");
const cors = require("cors");
// ----------------------------------
// this is where we add bcryptjs for our
// use...
const bcrypt = require("bcryptjs");
// ----------------------------------
const db = require("./data/dbConfig.js");