Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var PORT = 4000;
- var mysql = require('mysql');
- var con = mysql.createConnection({
- host: "localhost",
- user: "root",
- password: "",
- database: "EmployeesDB"
- });
- con.connect(function (err) {
- if (err) throw err;
- console.log("Connected!");
- });
- http.createServer( function(req, res ) {
- if(req.method == 'GET'){
- console.log(req.url);
- if(req.url.startsWith('/employees?')) {
- /*if indexOf('&') onda split('&')*/
- var value=req.url.split('?')[1].split('&'),
- limit=100,
- offset=1;
- if(value[0].startsWith('limit')){
- limit=parseInt(value[0].split('=')[1]);
- }
- if(value[0].startsWith('offset')){
- offset=parseInt(value[0].split('=')[1]);
- }
- if(value[1].startsWith('limit')){
- limit=parseInt(value[1].split('=')[1]);
- }
- if(value[1].startsWith('offset')){
- offset=parseInt(value[1].split('=')[1]);
- }
- console.log(value);
- console.log(offset);
- var sql = "SELECT first_name,emp_no FROM employees LIMIT ? OFFSET ?";
- con.query(sql, [limit,offset],function (err, result) {
- if (err) throw err;
- console.log(result);
- var r = "";
- for(var i=0;i<result.length;i++){
- r+= result[i].emp_no+result[i].first_name+'\n';
- }
- res.write(r);
- res.end();
- });
- }
- }
- } ).listen(PORT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement