Last active
August 9, 2018 00:07
-
-
Save BananaAcid/e41c5c585d74ea514cfc0cc89d4345f7 to your computer and use it in GitHub Desktop.
check if url is ok - my awaitable async solution
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
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(); | |
}); |
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 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