Created
June 9, 2017 04:43
-
-
Save truongngoclinh/2c142e0e545037d0d3d6497ec376dfc5 to your computer and use it in GitHub Desktop.
Sample to create own server working with iOS app on rest api
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
// | |
// HTTPService.h | |
// Tutorials | |
// | |
// Created by Linh on 6/7/17. | |
// Copyright © 2017 Linh. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
typedef void (^onComplete)(NSArray * __nullable dataArray, NSString* __nullable errMsg); | |
@interface HTTPService : NSObject | |
+ (id)instance; | |
- (void)getTutorials:(nullable onComplete)completionHandler; | |
- (void)postCommentsWithComment:(nonnull NSDictionary*)comment :(nullable onComplete)completionHandler; | |
@end |
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
// | |
// HTTPService.m | |
// Tutorials | |
// | |
// Created by Linh on 6/7/17. | |
// Copyright © 2017 Linh. All rights reserved. | |
// | |
#import "HTTPService.h" | |
#define URL_BASE "http://localhost:6069" | |
#define URL_TUTORIAL "/tutorials" // get | |
#define URL_COMMENT "/comments" // post | |
@implementation HTTPService | |
+ (id)instance { | |
static HTTPService *shared = nil; | |
@synchronized (self) { | |
if (shared == nil) { | |
shared = [[self alloc] init]; | |
} | |
return shared; | |
} | |
} | |
- (void)getTutorials:(nullable onComplete)completionHandler { | |
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%s%s", URL_BASE, URL_TUTORIAL]]; | |
NSURLSession *session = [NSURLSession sharedSession]; | |
[[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { | |
if (data != nil) { | |
NSError *err; | |
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err]; | |
if (err == nil) { | |
// NSLog(@"JSON: %@", json.debugDescription); | |
completionHandler(json, nil); | |
} else { | |
completionHandler(nil, err.debugDescription); | |
} | |
} else { | |
NSLog(@"NETWORK ERR: %@", error.debugDescription); | |
completionHandler(nil, @"Problem connecting to the server"); | |
} | |
}] resume]; | |
} | |
- (void)postCommentsWithComment:(nonnull NSDictionary*)comment :(nullable onComplete)completionHandler { | |
NSLog(@"postCommentsWithComment"); | |
NSError *error; | |
NSURLSession *session = [NSURLSession sharedSession]; | |
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%s%s", URL_BASE, URL_COMMENT]]; | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | |
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
[request setHTTPMethod:@"POST"]; | |
NSData *postData = [NSJSONSerialization dataWithJSONObject:comment options:0 error:&error]; | |
[request setHTTPBody:postData]; | |
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { | |
NSLog(@"postCommentsWithComment received data!"); | |
if (data != nil) { | |
NSError *err; | |
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
NSMutableArray *arr = [[NSMutableArray alloc] init]; | |
[arr addObject:strData]; | |
NSLog(@"valid data: %@", strData.debugDescription); | |
if (err == nil) { | |
// NSLog(@"JSON: %@", json.debugDescription); | |
completionHandler([NSArray arrayWithArray:arr], nil); | |
} else { | |
completionHandler(nil, err.debugDescription); | |
} | |
} else { | |
NSLog(@"NETWORK ERR: %@", error.debugDescription); | |
completionHandler(nil, @"Problem connecting to the server"); | |
} | |
}]; | |
[postDataTask resume]; | |
} | |
@end |
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 express = require('express'); | |
var bodyParser = require('body-parser'); | |
var app = express(); | |
app.all('/*', function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "X-Requested-With", "Content-Type, Accept"); | |
res.header("Access-Control-Allow-Methods", "POST", "GET"); | |
next(); | |
}); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
var tutorials = [ | |
{ | |
id: 1, | |
title: "Android Studio Tutorial for Beginners", | |
description: "Learn how to install AS", | |
iframe: '<div class="container"><iframe class="video" src="https://www.youtube.com/embed/nfs8NYg7yQM" frameborder="0" allowfullscreen></iframe></div>', | |
thumbnail: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQAaGEkWCL2oB8rMoa7aj8azJNGj_-HZvCWDbLOUP1XN18vBFrH" | |
}, | |
{ | |
id: 1, | |
title: "Android Kotlin Tutorial", | |
description: "Learn how to learn Kotlin", | |
iframe: '<div class="container"><iframe class="video" src="https://www.youtube.com/embed/nfs8NYg7yQM" frameborder="0" allowfullscreen></iframe></div>', | |
thumbnail: "https://s-media-cache-ak0.pinimg.com/originals/c5/7e/a0/c57ea04f0b2cbdeab1d94a1d0352dfbc.jpg" | |
} | |
]; | |
var comments = [ | |
{ | |
username: "linhtruong", | |
comment: "This is very cool practice deloying my own server" | |
} | |
]; | |
/* | |
app.put('/comments', function(req, res) { | |
var someObj = req.body; | |
var id = someObj.uniqueId; | |
// talk to database, find the record by the id | |
// then replace the existing record with req.body | |
res.send("Successfully update"); | |
}); | |
*/ | |
app.post('/comments', function(req, res) { | |
console.log("POST to server"); | |
var comment = req.body; | |
if (comment) { | |
if (comment.username && comment.comment) { | |
comments.push(comment); | |
console.log(comments); | |
} else { | |
res.send("You posted invalid data!"); | |
} | |
} else { | |
res.send("Your post has no body!"); | |
} | |
res.send("You successfully posted a comment!"); | |
}); | |
app.get('/tutorials', function(req, res) { | |
console.log("GET from server"); | |
res.send(tutorials); | |
}); | |
app.listen(6069); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment