mirror of
https://github.com/healthchecks/healthchecks.git
synced 2024-11-23 07:57:39 +00:00
b9996e63c8
HTML files in /templates/docs/ are not Django templates, they contain HTML content to be used verbatim in hc.front.views.serve_doc view. Some of these files contain "{{ ... }}" syntax. When we run "./manage.py compress", django-compressor trips up on this syntax because it treats them as Django templates. The fix is to change file extension for these files from .html to something else (I picked .html-fragment) so django-compressor would ignore them.
48 lines
6.5 KiB
Plaintext
48 lines
6.5 KiB
Plaintext
<h1>Javascript</h1>
|
|
<p>Below is a minimal example of making an HTTP request to SITE_NAME from Node.js.</p>
|
|
<div class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">https</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">require</span><span class="p">(</span><span class="s1">'https'</span><span class="p">);</span>
|
|
<span class="nx">https</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'PING_URL'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">=></span><span class="w"> </span><span class="p">{</span>
|
|
<span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Ping failed: '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">err</span><span class="p">)</span>
|
|
<span class="p">});</span>
|
|
</code></pre></div>
|
|
|
|
<p>Note: the "https" library executes requests asynchronously. If you send both "start"
|
|
and "success" signals, you can encounter a race condition where the "success" signal
|
|
arrives before the "start" signal. Avoid the race condition by using callbacks,
|
|
promises or the async/await feature. Here is an example that uses async/await and the
|
|
<a href="https://axios-http.com/">axios</a> library:</p>
|
|
<div class="highlight"><pre><span></span><code><span class="kd">const</span><span class="w"> </span><span class="nx">axios</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">require</span><span class="p">(</span><span class="s2">"axios"</span><span class="p">);</span>
|
|
|
|
<span class="k">async</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">ping</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
|
|
<span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">axios</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">url</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="nx">timeout</span><span class="o">:</span><span class="w"> </span><span class="mf">5000</span><span class="p">});</span>
|
|
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="p">(</span><span class="nx">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
<span class="w"> </span><span class="c1">// Log the error and continue. A ping failure should</span>
|
|
<span class="w"> </span><span class="c1">// not prevent the job from running.</span>
|
|
<span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="s2">"Ping failed: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">error</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="p">}</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">async</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nx">runJob</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
|
|
<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">pingUrl</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"PING_URL"</span><span class="p">;</span>
|
|
|
|
<span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">ping</span><span class="p">(</span><span class="nx">pingUrl</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"/start"</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
|
|
<span class="w"> </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"TODO: run the job here"</span><span class="p">);</span>
|
|
|
|
<span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">ping</span><span class="p">(</span><span class="nx">pingUrl</span><span class="p">);</span><span class="w"> </span><span class="c1">// success</span>
|
|
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="p">(</span><span class="nx">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
<span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">ping</span><span class="p">(</span><span class="nx">pingUrl</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">"/fail"</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="p">}</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="nx">runJob</span><span class="p">();</span>
|
|
</code></pre></div>
|
|
|
|
<h2>Browser</h2>
|
|
<p>You can also send pings from a browser environment. SITE_NAME sets the
|
|
<code>Access-Control-Allow-Origin:*</code> CORS header, so cross-domain AJAX requests work.</p>
|
|
<div class="highlight"><pre><span></span><code><span class="kd">var</span><span class="w"> </span><span class="nx">xhr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">XMLHttpRequest</span><span class="p">();</span>
|
|
<span class="nx">xhr</span><span class="p">.</span><span class="nx">open</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">,</span><span class="w"> </span><span class="s1">'PING_URL'</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span><span class="p">);</span>
|
|
<span class="nx">xhr</span><span class="p">.</span><span class="nx">send</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span>
|
|
</code></pre></div> |