Created
April 6, 2017 05:17
-
-
Save chriswebb09/59e96cd2e2c086b83540b5156d6c7513 to your computer and use it in GitHub Desktop.
Node Server
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
import UIKit | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
class APIClient { | |
typealias JSON = [String: Any] | |
static func search(for query: String, page: Int, completion: @escaping (_ responseObject: JSON?, _ error: Error?) -> Void) { | |
if let encodedQuery = query.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), | |
let url = URL(string:"http://localhost:3000/") { | |
APIClient.downloadData(url: url) { data, response, error in | |
if let error = error { | |
completion(nil, error) | |
} else { | |
if let data = data, let responseObject = self.convertToJSON(with: data) { | |
DispatchQueue.main.async { | |
completion(responseObject, nil) | |
} | |
} else { | |
completion(nil, NSError.generalParsingError(domain: url.absoluteString)) | |
} | |
} | |
} | |
} | |
} | |
fileprivate static func downloadData(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) { | |
URLSession(configuration: .ephemeral).dataTask(with: URLRequest(url: url)) { data, response, error in | |
completion(data, response, error) | |
}.resume() | |
} | |
fileprivate static func convertToJSON(with data: Data) -> JSON? { | |
do { | |
return try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? JSON | |
} catch { | |
return nil | |
} | |
} | |
} | |
extension NSError { | |
static func generalParsingError(domain: String) -> Error { | |
return NSError(domain: domain, code: 400, userInfo: [NSLocalizedDescriptionKey : NSLocalizedString("Error retrieving data", comment: "General Parsing Error Description")]) | |
} | |
} | |
APIClient.search(for: "new", page: 1) { movieData, error in | |
print(movieData) | |
} |
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
var http = require('http'); | |
var url=require('url'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var port = 3000 | |
var path = path.join(__dirname, 'movie.json'); | |
var server = http.createServer((request, response) => { | |
fs.readFile(path, function(error, data) { | |
switch(url.parse(request.url).pathname) { | |
case '/p1': | |
if (!error) { | |
response.end(data); | |
} else { | |
console.log(error); | |
} | |
case '/p2': | |
if (!error) { | |
response.end("Page 2"); | |
} else { | |
console.log(error); | |
} | |
default: | |
response.end("Default") | |
} | |
}) | |
}).listen(port, (error) => { | |
if (error) { | |
console.log(`error ${ error }`) | |
} | |
console.log(`listening on ${ port }`) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment