This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const AWS = require('aws-sdk'); | |
const fs = require('fs'); | |
const fileType = require('file-type'); | |
const multiparty = require('multiparty'); | |
// NOTE: if you're transpiling, using TS or don't use commonjs for any other reason, you can import instead of require: | |
// import express from 'express'; | |
// import AWS, { S3 } from 'aws-sdk'; | |
// import fs from 'fs'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: yarn serve |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
language: node_js | |
node_js: | |
- 10 | |
- 8 | |
services: | |
- mongodb | |
env: | |
- AUTH_SHARED_SECRET=auth-shared-secret | |
script: | |
- yarn lint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private getTestData = async (): Promise<void> => { | |
try { | |
this.setState({ error: "" }); | |
const response = await axios.get<Item[]>("/api/items", { headers: session.getAuthHeaders() }); | |
this.setState({ data: response.data }); | |
} catch (error) { | |
this.setState({ error: "Something went wrong" }); | |
} finally { | |
this.setState({ isRequesting: false }); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set the session in the local storage | |
export const setSession = (token: string, expiry: string): void => { | |
localStorage.setItem('token', token); | |
localStorage.setItem('expiry', expiry); | |
}; | |
// Clear the session from the local storage | |
export const clearSession = (): void => { | |
localStorage.removeItem('token'); | |
localStorage.removeItem('expiry'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MongodbMemoryServer from "mongodb-memory-server"; | |
import * as mongoose from "mongoose"; | |
import * as request from "supertest"; | |
import app from "../app"; | |
import User from "../users/user.model"; | |
import Item from "./item.model"; | |
describe("/api/items tests", () => { | |
const mongod = new MongodbMemoryServer(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as jwt from "express-jwt"; | |
// A-ha! So this is where the AUTH_SHARED_SECRET from .env is used! | |
export const authorize = jwt({ | |
secret: process.env.AUTH_SHARED_SECRET | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as bodyParser from "body-parser"; | |
import * as express from "express"; | |
import { authorize } from "../config"; | |
import Item from "./item.model"; | |
const router = express.Router(); | |
router.route("/").get(authorize, async (request, response) => { | |
const items = await Item.find(); | |
return response.status(200).json(items); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as mongoose from "mongoose"; | |
// Declare model interface | |
interface Item extends mongoose.Document { | |
name: string; | |
value: number; | |
} | |
// Define model schema | |
const itemSchema = new mongoose.Schema({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as dotenv from "dotenv"; | |
import * as express from "express"; | |
import * as path from "path"; | |
// Put dotenv in use before importing controllers | |
dotenv.config(); | |
// Import controllers | |
import itemsController from "./items/items.controller"; | |
import usersController from "./users/users.controller"; |
NewerOlder