-
-
Save HereIsJade/7fff631c09b8a892e0b64303e9dc0665 to your computer and use it in GitHub Desktop.
JSONP demo
This file contains 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
100 |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JSONP demo</title> | |
</head> | |
<body> | |
<p>Your current balance is $<span id="amount">$$$amount$$$</span></p> | |
<button id="payBtn">Pay $1</button> | |
<script> | |
payBtn.addEventListener('click',()=>{ | |
let script = document.createElement('script') | |
let functionName = `my${parseInt(Math.random()*10000000 ,10)}` | |
window[functionName] = (res)=>{ | |
console.log(res) | |
if(res.success){ | |
amount.innerText = res.amount | |
} | |
} | |
console.log(window[functionName]) | |
script.src = `http://jack.com:8002/pay?callback=${functionName}` | |
document.body.appendChild(script) | |
script.onload = (e)=>{ | |
e.currentTarget.remove() | |
delete window[functionName] | |
} | |
script.onerror = (e)=>{ | |
alert('fail') | |
e.currentTarget.remove() | |
delete window[functionName] | |
} | |
}) | |
</script> | |
</body> | |
</html> |
This file contains 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 http = require('http') | |
let fs = require('fs') | |
let url = require('url') | |
let port = process.argv[2] | |
if(!port){ | |
console.log('Please specify a port number\nExample: node server.js 8888') | |
process.exit(1) | |
} | |
let server = http.createServer(function(request, response){ | |
let parsedUrl = url.parse(request.url, true) | |
let pathWithQuery = request.url | |
let queryString = '' | |
if(pathWithQuery.indexOf('?') >= 0){ queryString = pathWithQuery.substring(pathWithQuery.indexOf('?')) } | |
let path = parsedUrl.pathname | |
let query = parsedUrl.query | |
let method = request.method | |
console.log('HTTP path is\n' + pathWithQuery) | |
if(path === '/'){ | |
let HTMLstring = fs.readFileSync('./index.html', 'utf-8') | |
let amount = fs.readFileSync('./db', 'utf-8') // read from db | |
HTMLstring = HTMLstring.replace('$$$amount$$$', amount) | |
response.statusCode = 200 | |
response.setHeader('Content-Type', 'text/html;charset=utf-8') | |
response.write(HTMLstring) | |
response.end() | |
} | |
else if(path === '/pay'){ | |
let amount = fs.readFileSync('./db', 'utf-8') // read from db | |
let newBalance = amount - 1 | |
fs.writeFileSync('./db', newBalance) | |
response.setHeader('Content-Type', 'application/javascript') | |
response.statusCode = 200 | |
response.write(` | |
${query.callback}({ | |
'success': true, | |
'amount': ${newBalance} | |
}) | |
`) | |
response.end() | |
} | |
else{ | |
response.statusCode = 404 | |
response.setHeader('Content-Type', 'text/html;charset=utf-8') | |
response.write('404 not found') | |
response.end() | |
} | |
}) | |
server.listen(port) | |
console.log('Listening on ' + port + ' success\nPlease visit http://localhost:' + port) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment