├── README.md ├── package.json ├── nodejs.sql └── Dapi.js /README.md: -------------------------------------------------------------------------------- 1 | # dynamic-api 2 | its my frist dynamic api for country , continent and Capital 3 | it worked with mysql 4 | i put the database file you can test it 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dynamic-api", 3 | "version": "1.0.0", 4 | "description": "my frist dynamic api for countries", 5 | "main": "Dapi.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/nightcode-dev/dynamic-api.git" 12 | }, 13 | "keywords": [ 14 | "dynamic", 15 | "api", 16 | "city", 17 | "country", 18 | "continent" 19 | ], 20 | "author": "nightcode", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/nightcode-dev/dynamic-api/issues" 24 | }, 25 | "homepage": "https://github.com/nightcode-dev/dynamic-api#readme", 26 | "dependencies": { 27 | "mysql": "^2.18.1" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /nodejs.sql: -------------------------------------------------------------------------------- 1 | -- phpMyAdmin SQL Dump 2 | -- version 5.1.0 3 | -- https://www.phpmyadmin.net/ 4 | -- 5 | -- Host: localhost 6 | -- Generation Time: Nov 24, 2021 at 06:14 AM 7 | -- Server version: 5.6.38 8 | -- PHP Version: 7.4.3 9 | 10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 | START TRANSACTION; 12 | SET time_zone = "+00:00"; 13 | 14 | 15 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 16 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 17 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 18 | /*!40101 SET NAMES utf8mb4 */; 19 | 20 | -- 21 | -- Database: `nodejs` 22 | -- 23 | 24 | -- -------------------------------------------------------- 25 | 26 | -- 27 | -- Table structure for table `ApiJson` 28 | -- 29 | 30 | CREATE TABLE `ApiJson` ( 31 | `country` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL, 32 | `city` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL, 33 | `continent` varchar(100) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL 34 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 35 | COMMIT; 36 | 37 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 38 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 39 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 40 | -------------------------------------------------------------------------------- /Dapi.js: -------------------------------------------------------------------------------- 1 | //my Required 2 | const http = require("http"); 3 | const fs = require("fs"); 4 | const qs = require('querystring'); 5 | const mysql = require('mysql'); 6 | 7 | var api = []; 8 | 9 | //make a con to read and put data to api 10 | var con = mysql.createConnection({ 11 | host: "localhost", 12 | user: "root", 13 | password: "", 14 | database: "nodejs" 15 | }); 16 | 17 | //connect... 18 | con.connect(function(err){ 19 | 20 | //check error to connect 21 | if(err) 22 | { 23 | 24 | //if be error 25 | throw err 26 | } 27 | else 28 | { 29 | 30 | //if not errored 31 | //sql code and query func to get json api codes 32 | var sql=`SELECT * FROM ApiJson`; 33 | con.query(sql,function(err,result,fields) 34 | { 35 | 36 | //check query working 37 | if(err){ 38 | 39 | //when be errored 40 | throw err; 41 | }else{ 42 | 43 | //if not be errored 44 | api = result 45 | } 46 | } 47 | ); 48 | } 49 | } 50 | ); 51 | 52 | //main Server 53 | http.createServer(function(rq,rs) 54 | { 55 | 56 | var puted = router(rs,rq,api); 57 | 58 | if(puted.length == 0) 59 | { 60 | //make a form to send data to api database 61 | if(rq.url === "/sub" && rq.method === "GET") 62 | { 63 | //if request method is get and request url is /sub 64 | rs.writeHead(200,{"Content-Type":"text/html"}); 65 | fs.createReadStream("./public/index.html","UTF-8").pipe(rs); 66 | } 67 | else if(rq.url === "/sub" && rq.method === "POST") 68 | { 69 | //if request method post and request url /sub 70 | var SDATA; 71 | rq.on("data",function(chunk) 72 | { 73 | 74 | //get posted string chunk by chunk 75 | SDATA += chunk; 76 | 77 | } 78 | ); 79 | rq.on("end",function(chunk) 80 | { 81 | 82 | //if reading chunk ended 83 | rs.writeHead(200,{"Content-Type":"text/html"}); 84 | 85 | //parse posted string 86 | var POSTED = qs.parse(SDATA); 87 | 88 | //make a con to send posted data to server 89 | var con = mysql.createConnection( 90 | { 91 | host: "localhost", 92 | user: "root", 93 | password: "", 94 | database: "nodejs" 95 | } 96 | ); 97 | 98 | //connect... 99 | con.connect(function(err){ 100 | 101 | //check error to connect 102 | if(err) 103 | { 104 | 105 | //if be error 106 | throw err 107 | } 108 | else 109 | { 110 | 111 | //make sql code to insert posted data into database 112 | var sql=`INSERT INTO ApiJson (country,City,continent) VALUES ('${POSTED.undefinedcountry}', '${POSTED.city}', '${POSTED.continent}');`; 113 | con.query(sql,function(err,result) 114 | { 115 | 116 | //check error and result 117 | if(err) 118 | { 119 | 120 | //if be errored 121 | rs.end(` 122 | 123 | 124 | 125 | 126 | 127 | post example in nodejs 128 | 129 | 130 |

${err}

131 | 132 | `); 133 | 134 | } 135 | else 136 | { 137 | 138 | //if not be errored 139 | rs.end(` 140 | 141 | 142 | 143 | 144 | 145 | post example in nodejs 146 | 147 | 148 |

ثبت شد

149 | 150 | `); 151 | console.log(JSON.stringify(result)); 152 | 153 | } 154 | } 155 | ); 156 | 157 | //sql code and query func to get json api codes 158 | var sql=`SELECT * FROM ApiJson`; 159 | con.query(sql,function(err,result,fields) 160 | { 161 | 162 | //check query working 163 | if(err){ 164 | 165 | //when be errored 166 | throw err; 167 | 168 | }else{ 169 | 170 | //if not be errored 171 | api = result 172 | } 173 | } 174 | ); 175 | } 176 | 177 | 178 | 179 | } 180 | ); 181 | } 182 | ); 183 | 184 | }else if(rq.url === "/"){ 185 | rs.writeHead(200,{"Content-Type":"text/json"}); 186 | rs.end(JSON.stringify(api)) 187 | }else{ 188 | rs.writeHead(404,{"Content-Type":"text/plain"}); 189 | rs.end("oops...!your record not founded"); 190 | } 191 | 192 | console.log(`request for ${rq.url} with ${rq.method} method`); 193 | }else{ 194 | rs.writeHead(200,{"Content-Type":"text/json"}); 195 | rs.end(JSON.stringify(puted)) 196 | } 197 | 198 | }).listen(3000); 199 | 200 | //mini router function 201 | function router(rs,rq,json){ 202 | var puted = []; 203 | json.forEach(function(obj) 204 | { 205 | 206 | //mini router 207 | if(rq.url === `/${obj.country}`){ 208 | puted.push(obj); 209 | 210 | }else if(rq.url === `/${obj.continent}`){ 211 | puted.push(obj); 212 | 213 | } 214 | 215 | } 216 | ); 217 | 218 | return puted; 219 | } 220 | --------------------------------------------------------------------------------