Summary of APNs functions in Node. js and Mysql applications

Source: Internet
Author: User
Tags install node

This document mainly summarizes the learning experience of Node. js and Mysql. Of course, it can also be seen as the continuation of the previously written message PUSH Service. Briefly describe the background of the application. Our application needs to implement Apple's message PUSH Service APNs. Previously, we have implemented iOS client configuration and function code, as well as the local Provider function code for pushing notifications, for more information, see previous summary. Like A triangle, point A represents the iOS mobile device, point B represents Apple's message push server, and point C represents the local server of the application developer. Now A and B are connected, if B and C are connected, A and B are connected. In this way, it is obvious that A server needs to be built locally to process requests sent by A. Of course, the database is also indispensable for storing relevant data. Here, people with different technical backgrounds will have different solutions. Here I use the Node. js + Mysql solution. Of course, other solutions, such as ASN. NET, JSP, PHP, and SQLServer, Mysql, and MongoDB, will not be discussed. It serves as a pure attempt to learn technology and new solutions. Node. js, as the key content of this document, must be summarized first. I am a newbie who just learned Node. js. I am afraid to use this article as an entry-level document for Node. js. I just want to briefly introduce it and then implement code based on application requirements. Node. js implements server-side Javascript. By setting up a simple and quick environment, you can implement a server that runs Javascript. 1. Go to the official website to download the latest Node. js installer. Note: I installed Windows on a 32-bit operating system, so download the corresponding installation program for Windows. My version is: node-v0.10.24-x86. 2. Install Node. js. 3. In start-> all programs, find the Node. js command line program Node. js command prompt and run it. Now you can start programming. This is simple and quick. Maybe many people are still confused. Where can I write code and use the command line? Here, I want to talk about my feelings when I first came into contact: I felt very depressed and did not know where to start. For many new users, what you need is to clarify things in a bit, rather than a mystery. It can be understood that Node. js is a server that carries everything that Javascript, CSS, Html, and other webpages can have. However, it is not integrated with IDE. Next, we can deploy IIS, Apache, and Tomcat without having to run Node. js, and then click "node xxx. js ", load the Javascript that has been written, everything is OK. Yes, Javascript. use any method or tool you are familiar. Notepad ++ is the tool I chose. There must be some questions. It doesn't matter. Let's first look at the instance. The following example provides a page for uploading and previewing image files, and a request for inserting data into the database. First, let's look at the folder structure of NodejsDemo in this example: index. js is the portal, server. js is used to start the service, router. js is used to route requests, requesHandlers. js is used to process the corresponding request after the route, mysqlOperation. js is used to process database operations, mysqlDBHelper. js is used to connect to the database and perform operations. The next step is the specific code section. I Can't clarify too many syntaxes. I will explain it a little while where I need it and want to know more, we recommend that you go to the official website to view the API instructions. Index. js Code: Copy code 1 var server = require (". /server "); 2 3 var router = require (". /router "); 4 5 var requestHandlers = require (". /requestHandlers "); 6 7 8 9 var handle = {} 10 11 handle ["/"] = requestHandlers. start; 12 13 handle ["/start"] = requestHandlers. start; 14 15 handle ["/upload"] = requestHandlers. upload; 16 17 handle ["/show"] = requestHandlers. show; 18 19 handle ["/sendMyInfo"] = requestHandlers. SendMyInfo; 20 21 22 23 server. start (router. route, handle); copy the Code Description: This is the sample entry. In the last sentence, call the start method in server and pass two parameters. The first one is the route function in router, the second is the handle similar to the dictionary instance. Server. js Code: Copy code 1 var http = require ('http'); 2 3 var url = require ('url'); 4 5 function start (route, handle) {6 7 function onRequest (req, res) {8 9 var pathname = url. parse (req. url ). pathname; 10 11 console. log ("Request for" + pathname + "received. "); 12 13 14 15 route (handle, pathname, res, req); 16 17} 18 19 http. createServer (onRequest ). listen (1337, 'Your ip'); 20 21 console. log ('server running At http: // Your IP: 1337/'); 22 23} 24 25 26 27 exports. start = start; copy the Code Description: With require (". /server ") different from the js files in the same directory. require ('HTTP ') is the method for generating an http object instance. Url. parse (string). pathname is used to obtain the path name in the url string. The Your IP address is the local IP address. Http. createServer (function). listen (port number, 'IP') is used to create a service in one way and listen to the configured IP address and port number. Exports. start = start is another js object that calls this method to expose the interface. Route. js Code copy code 1 function route (handle, pathname, res, req) {2 3 console. log ("About to route a request for" + pathname); 4 5 if (typeof handle [pathname] = 'function') {6 7 handle [pathname] (res, req); 8 9} else {10 11 console. log ("No request handler found for" + pathname); 12 13 res. writeHead (404, {'content-type': 'text/plain '}); 14 15 res. end ('2014 route not found '); 16 17} 18 19} 20 21 22 23 expo Rts. route = route; copy Code Description: route request. When the requested path is undefined, Error 404 is returned in the response head. RequestHanlders. js Code: Copy code 1 var querystring = require ("querystring"); 2 3 var url = require ('url '); 4 5 var fs = require ("fs"); 6 7 var formidable = require ("formidable"); 8 9 var util = require ("util "); 10 11 var mysqlOperation = require (". /mysqlOperation "); 12 13 var testPicPath ="/testPic/test.png "; 14 15 function start (res, req) {16 17 console. log ("Request handler 'start' was called. "); 18 19 var body = '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.