- Error Tracking https://sentry.io
- Error Tracking https://logrocket.com
- Localisation https://crowdin.com
- Localisation https://poeditor.com
- Auth https://auth0.com
- Continuous Integration https://travis-ci.org/
- E2E Testing https://www.cypress.io
- Cross Browser Testing https://www.browserstack.com
- Code Coverage https://codecov.io
- Serverless https://zeit.co/
| //file: ./pages/api/checkHTTPMethod.ts | |
| /* this code will allow only GET method requests on this route */ | |
| import { Middleware, use } from 'next-api-route-middleware'; | |
| import type { NextApiRequest, NextApiResponse } from 'next'; | |
| export const allowMethods = (allowedMethods: string[]): Middleware => { | |
| return async function (req, res, next) { |
| import { NextResponse } from 'next/server' | |
| import type { NextRequest } from 'next/server' | |
| export function middleware(req: NextRequest) { | |
| // get cookie token | |
| const hasToken = req.cookies.get('token') | |
| // protected routes (admin routes) | |
| if (req.nextUrl.pathname.startsWith('/admin')) { | |
| if (hasToken) { |
| /** | |
| * Converts paths defined in tsconfig.json to the format of | |
| * moduleNameMapper in jest.config.js. | |
| * | |
| * For example, {'@alias/*': [ 'path/to/alias/*' ]} | |
| * Becomes {'@alias/(.*)': [ '<rootDir>/path/to/alias/$1' ]} | |
| * | |
| * @param {string} srcPath | |
| * @param {string} tsconfigPath | |
| */ |
| main() { | |
| var city = "new york"; | |
| print(titleCase(city)); | |
| } | |
| /// Inefficient way of capitalizing each word in a string. | |
| String titleCase(String text) { | |
| if (text.length <= 1) return text.toUpperCase(); | |
| var words = text.split(' '); | |
| var capitalized = words.map((word) { |
| version: "3.5" | |
| services: | |
| mongo: | |
| image: mongo:latest | |
| container_name: mongo | |
| environment: | |
| MONGO_INITDB_ROOT_USERNAME: admin | |
| MONGO_INITDB_ROOT_PASSWORD: admin | |
| ports: |
| handleDrop = files => { | |
| // Push all the axios request promise into a single array | |
| const uploaders = files.map(file => { | |
| // Initial FormData | |
| const formData = new FormData(); | |
| formData.append("file", file); | |
| formData.append("tags", `codeinfuse, medium, gist`); | |
| formData.append("upload_preset", "pvhilzh7"); // Replace the preset name with your own | |
| formData.append("api_key", "1234567"); // Replace API key with your own Cloudinary key | |
| formData.append("timestamp", (Date.now() / 1000) | 0); |
The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.
In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.
This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.
Zach Caceres
Javascript does not have the typical 'private' and 'public' specifiers of more traditional object oriented languages like C# or Java. However, you can achieve the same effect through the clever application of Javascript's function-level scoping. The Revealing Module pattern is a design pattern for Javascript applications that elegantly solves this problem.
The central principle of the Revealing Module pattern is that all functionality and variables should be hidden unless deliberately exposed.
Let's imagine we have a music application where a musicPlayer.js file handles much of our user's experience. We need to access some methods, but shouldn't be able to mess with other methods or variables.