How to use where clause in Knex
Create refunds with Stripe
Load HTML with Cheerio
How to send POST request with Axios
Find intersection of array in Lodash
Powered by Official white Bloop logo with a period

Terms / Privacy / Search / Support

  • var accepts = require('accepts')
    var http = require('http')
     
    function app (req, res) {
      var accept = accepts(req)
     
      // the order of this list is significant; should be server preferred order
    // the order of this list is significant; should be server preferred order
      switch (accept.type(['json', 'html'])) {
        case 'json':
          res.setHeader('Content-Type', 'application/json')
          res.write('{"hello":"world!"}')
          break
        case 'html':
          res.setHeader('Content-Type', 'text/html')
          res.write('<b>hello, world!</b>')
          break
        default:
          // the fallback is text/plain, so no need to specify it above
    // the fallback is text/plain, so no need to specify it above
          res.setHeader('Content-Type', 'text/plain')
          res.write('hello, world!')
          break
      }
     
      res.end()
    }
     
    http.createServer(app).listen(3000)
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">function</span> <span class="token function">accepts</span><span class="token punctuation">(</span><span class="token parameter">req<span class="token operator">:</span> IncomingMessage</span><span class="token punctuation">)</span><span class="token operator">:</span> accepts<span class="token punctuation">.</span><span class="token function">Accepts</span> <span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">namespace</span> accepts <span class="token keyword">import</span> accepts</code></pre>
    Docs
    0
  • constructor(event) {
        
        //...
    this.ips = (this.get('X-Forwarded-For') || '').split(', ');
        this.ip = this.ips[0];
        this.host = this.get('X-Forwarded-Host') || this.hostname;
        this.xhr = (this.get('X-Requested-With') || '').toLowerCase() === 'xmlhttprequest';
    
        this.url = `${this.headers}${this.path}`;
    
        this.event = event;
        this.accept = accepts(this);
    
        this.push(event.body);
        this.push(null);
      }
    
      
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript">constructor <span class="token function">Request</span><span class="token punctuation">(</span>event<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">)</span><span class="token operator">:</span> Request</code></pre>
    /muratcorlu/lambda-expressless mit
    0
  • const reactIntlLocaleMessages = function (req, res, next) {
        const accept = accepts(req);
        const locale = accept.language(languages) || "en";
        req.locale = locale;
        req.localeDataScript = getLocaleDataScript(locale);
        req.messages = getMessages(locale);
        next();
    };
    
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> <span class="token function-variable function">reactIntlLocaleMessages</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token operator">:</span> any<span class="token punctuation">,</span> res<span class="token operator">:</span> any<span class="token punctuation">,</span> next<span class="token operator">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">void</span></code></pre>
    /Enalmada/next-reason-boilerplate MIT
    0
  • function Request (ctx) {
        this.ctx = ctx
        this.req = ctx.req
        this.res = ctx.res
        this.accept = accepts(ctx.req)
        this.originalUrl = ctx.req.url
      }
      
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>local <span class="token keyword">function</span><span class="token punctuation">)</span> <span class="token function">Request</span><span class="token punctuation">(</span>ctx<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span></code></pre>
    /toajs/toa mit
    0
  • const frontend = async (req, res, next) => {
    	if (!(await fs.isDir(req.absolute))) return next()
    	if (accepts(req).type('html') !== 'html') return next()
    
    	let files = (await fs.readDir(req.absolute))
    	.map((name) => ({
    		name, path: '/' + path.join(req.relative, name)
    	}))
    
    	
    
    	//...
    }
    
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> <span class="token function-variable function">frontend</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token operator">:</span> any<span class="token punctuation">,</span> res<span class="token operator">:</span> any<span class="token punctuation">,</span> next<span class="token operator">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token builtin">Promise</span><span class="token operator">&lt;</span><span class="token builtin">any</span><span class="token operator">></span></code></pre>
    /derhuerst/casket ISC
    0
  • const search = async (req, res, next) => {
    	
    	//...
    if (!(await fs.isDir(req.absolute))) return next()
    	if (accepts(req).type('html') !== 'html') return next() // todo: support JSON & plain text
    
    	let results = []
    	
    	//...
    }
    
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">module</span> search <span class="token keyword">const</span> search<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token punctuation">(</span>req<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">,</span> res<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">,</span> next<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Promise</span><span class="token operator">&lt;</span><span class="token builtin">any</span><span class="token operator">></span><span class="token punctuation">;</span> supported<span class="token operator">:</span> <span class="token builtin">boolean</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre>
    /derhuerst/casket ISC
    0
  • const setDateString = () => {
        const TYPE_TIMESTAMP = 1114
        const TYPE_TIMESTAMPTZ = 1184
        const TYPE_DATESTAMP = 1082
        const customParse = v => v.split('.')[0]
    
        types.setTypeParser(TYPE_TIMESTAMP, customParse)
        types.setTypeParser(TYPE_TIMESTAMPTZ, customParse)
        types.setTypeParser(TYPE_DATESTAMP, customParse)
    }
    
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> <span class="token function-variable function">setDateString</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token keyword">void</span></code></pre>
    /ib-ganz/eloquify mit
    0
  • //...
    if (!databaseUrl)
        throw new Error('must set DATABASE_URL environment var');
    
    console.log('DATABASE_URL: ', databaseUrl);
    
    pg.types.setTypeParser(20, function(val) { // parse int8 as an integer
        return val === null ? null : parseInt(val);
    });
    
    // callback is called with (err, client, done)
    function connect(callback) {
        return pg.connect(databaseUrl, callback);
    }
    
    
    
    //...
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">var</span> databaseUrl<span class="token operator">:</span> <span class="token builtin">string</span></code></pre>
    /Dexon95/Bustabit agpl-3.0
    0
  • 
    //...
    const client = new Pool(options)
    
    const DATE_OID = 1082
    const NUMERIC_OID = 1700
    
    types.setTypeParser(DATE_OID, val => val)
    types.setTypeParser(NUMERIC_OID, val => parseFloat(val))
    
    
    
    //...
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">const</span> client<span class="token operator">:</span> Pool</code></pre>
    /jonathanong/ims mit
    0
  • //...
    var customDateParser = (val) => {
      if (val === null) {
        return null;
      } else {
        var date = moment.parseZone(val)
        date.origValueString = val
        return date
      }
    };
    
    types.setTypeParser(TIMESTAMPTZ_OID, customDateParser)
    types.setTypeParser(TIMESTAMP_OID, customDateParser)
    
    var usingNativeLib = false;
    // Disable loading pg-native until issue with openssl paths is solved
    
    //...
    
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token keyword">var</span> <span class="token function-variable function">customDateParser</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">val<span class="token operator">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=></span> moment<span class="token punctuation">.</span>Moment</code></pre>
    /Paxa/postbird MIT
    0
  • import React from 'react'
    import {render, screen} from '@testing-library/react'
    import userEvent from '@testing-library/user-event'
    
    test('type', () => {
      render(<textarea />)
    
      userEvent.type(screen.getByRole('textbox'), 'Hello,{enter}World!')
      expect(screen.getByRole('textbox')).toHaveValue('Hello,\nWorld!')
    })
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">namespace</span> React <span class="token keyword">import</span> React</code></pre>
    Docs
    0
  • import React from 'react'
    import {render, screen} from '@testing-library/react'
    import userEvent from '@testing-library/user-event'
    
    test('types into the input', () => {
      render(
        <>
          <label for="time">Enter a time</label>
          <input type="time" id="time" />
        </>,
      )
      const input = screen.getByLabelText(/enter a time/i)
      userEvent.type(input, '13:58')
      expect(input.value).toBe('13:58')
    })
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">namespace</span> React <span class="token keyword">import</span> React</code></pre>
    Docs
    0
  • import React from 'react'
    import {render, screen} from '@testing-library/react'
    import userEvent from '@testing-library/user-event'
    
    test('delete characters within the selectedRange', () => {
      render(
        <div>
          <label htmlFor="my-input">Example:</label>
          <input id="my-input" type="text" value="This is a bad example" />
        </div>,
      )
      const input = screen.getByLabelText(/example/i)
      input.setSelectionRange(10, 13)
      userEvent.type(input, '{backspace}good')
    
      expect(input).toHaveValue('This is a good example')
    })
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">namespace</span> React <span class="token keyword">import</span> React</code></pre>
    Docs
    0
  • import React from 'react'
    import {render, screen} from '@testing-library/react'
    import userEvent, {specialChars} from '@testing-library/user-event'
    
    test('delete characters within the selectedRange', () => {
      render(
        <div>
          <label htmlFor="my-input">Example:</label>
          <input id="my-input" type="text" value="This is a bad example" />
        </div>,
      )
      const input = screen.getByLabelText(/example/i)
      input.setSelectionRange(10, 13)
      userEvent.type(input, `${specialChars.backspace}good`)
    
      expect(input).toHaveValue('This is a good example')
    })
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>alias<span class="token punctuation">)</span> <span class="token keyword">namespace</span> React <span class="token keyword">import</span> React</code></pre>
    Docs
    0
  • walkSync(s3Path, (filePath, _stat) => {
          return new Promise(async (resolveWalk, rejectWalk) => {
            
            //...
    if (!['.svg', '.png', '.jpeg', '.jpg', '.gz'].includes(path.extname(filePath)) || filePath.includes('node_modules') || filePath.includes('.github')) {
              return resolveWalk();
            }
            const contentType = mime.contentType(path.extname(bucketPath));
            const params = {
              Bucket: bucketName,
              ACL: 'public-read',
              ContentType: contentType,
              Key: key,
              Body: fs.readFileSync(filePath)
            };
            const success = await s3.putObject(params).promise().catch(rejectWalk);
            
            //...
    });
        })
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token punctuation">(</span>local <span class="token keyword">function</span><span class="token punctuation">)</span> <span class="token function">walkSync</span><span class="token punctuation">(</span>currentDirPath<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">,</span> callback<span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">Promise</span><span class="token operator">&lt;</span><span class="token keyword">void</span><span class="token operator">></span></code></pre>
    /MaxvandeLaar/homey-community-store-cli gpl-3.0
    0
  • http_request = function (request, response) {
    
    //...
    fs.exists(filename, function(exists) {
            
            //...
    fs.readFile(filename, "binary", function(err, file) {
                if(err) {
                    return http_error(response, 500, err);
                }
    
                var headers = {};
                var contentType = mime.contentType(path.extname(filename));
                if (contentType !== false) {
                  headers['Content-Type'] = contentType;
                }
    
                
    
                //...
    });
        });
    }
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token builtin">any</span></code></pre>
    /VMDashboard/vmdashboard gpl-3.0
    0
  • mime.contentType('markdown') // 'text/x-markdown; charset=utf-8'
    mime.contentType('file.json') // 'application/json; charset=utf-8'
    mime.contentType('text/html') // 'text/html; charset=utf-8'
    mime.contentType('text/html; charset=iso-8859-1') // 'text/html; charset=iso-8859-1'
    
    // from a full path
    mime.contentType(path.extname('/path/to/file.json')) // 'application/json; charset=utf-8'
    <pre class="typing-tooltip"><code data-language="javascript" class="language-javascript"><span class="token builtin">any</span></code></pre>
    Docs
    0
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples