Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http'),
- fs = require('fs'),
- rl = require('readline'),
- mysql = require('mysql');
- var connection = mysql.createConnection({
- host : 'localhost',
- user : 'root',
- password : 'admin123',
- database : 'employees'
- });
- const PORT=8080;
- var server = http.createServer(handleRequest);
- var users = [];
- var tmpUser = {"first_name":"","last_name":"","email":"","country":"","ip_address":""};
- connection.connect();
- function handleRequest(request, response){
- var splittedUrl = splitUrl(request.url);
- //askUser();
- switch(request.method){
- case "GET":
- if (splittedUrl.length == 2) {
- if (isCollectionUrl(splittedUrl)) {
- connection.query('SELECT * FROM "employees"', function(error,results,fields){
- response.end(write(JSON.stringify(results)));
- });
- }
- else if (isElementUrl(splittedUrl)) {
- response.end();
- }
- }
- break;
- case "POST":
- console.log("Request method is POST");
- break;
- }
- }
- server.listen(PORT, function(){
- console.log("Server listening on: http://localhost:", PORT);
- });
- function splitUrl(url) {
- var array = url.split("/");
- array.shift();
- return array;
- }
- function isCollectionUrl(array){
- return (array[0] == "employees" && array[1] == "");
- }
- function isElementUrl(array) {
- return (array[0] == "employees" && array[1].match(/^\d+$/));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement