Category Archives: Node.js

D3.js

LEUNG    D3.js 沒有迴響

d3.js

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation.

Reference : https://d3js.org/

Create csv, xls file in NodeJS

csv.js

var mysql = require('mysql');
var express = require('express');
var csv = require('express-csv');
var app = express();
var Iconv = require("iconv").Iconv;  
var iconv = new Iconv('utf8', 'utf16le');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "test"
});

console.log('Creating the http server');
var content  = "";  
con.query('SELECT * from user', function(err, rows, fields) {	
	if (err) throw err; 
	for (var i in rows) {
		content += rows[i].name +"\t"
	}
	console.log(content);
	con.end();
});

app.get('/', function(req, res) {	  
  res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  res.setHeader("Content-Disposition", 'attachment; filename=itworks.csv');
  res.write(new Buffer([0xff, 0xfe]));
  res.write(iconv.convert(content));
  res.end();
});
app.listen(1000);

User.sql

CREATE TABLE `user` (
  `user_id` int(50) NOT NULL,
  `name` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`user_id`, `name`, `password`) VALUES
(1, '陳大文', 'A1234567'),
(2, '陳小文', 'B1234567'),
(3, '陳中文', 'C1234567');

Select query get row data in NodeJS

mysql.js

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "test"
});

var queryString = 'SELECT * FROM user';
 
con.query(queryString, function(err, rows, fields) {
	if (err) throw err; 
	for (var i in rows) {
		console.log('Username: ', rows[i].name);
	}
});
 
con.end();

Console Log:

Username:  陳大文
Username:  陳小文
Username:  陳中文

Select query output to Json format in NodeJS

User.sql

CREATE TABLE `user` (
  `user_id` int(50) NOT NULL,
  `name` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- 資料表的匯出資料 `user`
--

INSERT INTO `user` (`user_id`, `name`, `password`) VALUES
(1, '陳大文', 'A1234567'),
(2, '陳小文', 'B1234567'),
(3, '陳中文', 'C1234567');

mysql.js

var mysql = require('mysql');
var http =  require("http");

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "test"
});

con.connect();

http.createServer(function  (request, response)  {
	console.log('Creating the http server');
	con.query('SELECT * from user', function(err, rows, fields) {	
		response.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});  
		response.end(JSON.stringify(rows));	
	}); 
	
}).listen(1000);  

Output JSON :

[
 {"user_id":1,"name":"陳大文","password":"A1234567"},
 {"user_id":2,"name":"陳小文","password":"B1234567"},
 {"user_id":3,"name":"陳中文","password":"C1234567"}
]