Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active August 9, 2018 00:07
Show Gist options
  • Save BananaAcid/e41c5c585d74ea514cfc0cc89d4345f7 to your computer and use it in GitHub Desktop.
Save BananaAcid/e41c5c585d74ea514cfc0cc89d4345f7 to your computer and use it in GitHub Desktop.
check if url is ok - my awaitable async solution
let options = {
host: 'eposthub.de',
//port: 80, optional
//path: '/' optional
}
const http = require('http');
let isOk = await new Promise(resolve => {
http.request({method:'HEAD', host:options.host, port:options.port, path: options.path}, r =>
resolve(r.statusCode >= 200 && r.statusCode < 400)
).on('error', resolve).end();
});
const https = require('https');
const http = require('http');
function urlOk(options) {
return new Promise(resolve => {
options.protocol = options.protocol || 'http'
let r = options.protocol == 'http' ? http.request : https.request;
r({method:'HEAD', host:options.host, port:options.port, path: options.path}, r =>
resolve(r.statusCode >= 200 && r.statusCode < 400)
).on('error', resolve).end();
});
}
let options = {
port: 80, // optional
host: 'eposthub.de',
path: '/', // optional
protocol: 'http' // optional
}
let x = await urlOk(options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment