mysql


NodeJS

to avoid an issue, I installed homebrew which is missing package manager for MacOS
https://brew.sh/

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

and install mysql

brew update
brew search mysql
brew install mysql

start mysql server

mysql.server start

basic setting

mysql_secure installation

create a database on the console

create database nodejs;

create a table

create table user (
  id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  email varchar(255) NOT NULL,
  name varchar(255) NOT NULL,
  pw varchar(255) NOT NULL,
  PRIMARY KEY(id)
);

insert test date into table

insert into user (email,name,pw) values ('leeeuido@gmail.com', 'euido', 'asdf');

app.js

var express = require('express')
var app = express()
var bodyParser = require('body-parser')
// declare that we are going to use mysql
var mysql = require('mysql')

// mysql connection info (use default port)
var connection = mysql.createConnection({ host : 'localhost', port : 3306, user : 'root', password : 'SECRET', database : 'nodejs'
})

// make a connection
connection.connect()

app.listen(3000, function() {
console.log("start!! express server on port 3000"); });

app.use(express.static('public'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}))
app.set('view engine', 'ejs')


app.post ('/ajax_send_email', function(req, res){
  var email = req.body.email
  var responseData = {};
  // make a query
  var query = connection.query('select name from user where email="' + email + '"', function(err,rows){
    // if result has error, throw err
    if(err) throw err;
    // if rows[0] exists, print a result
    if(rows[0]) {
      responseData.result = "ok";
      responseData.name = rows[0].name;
      } else {
      responseData.result = "none";
      responseData.name = "";
      }
      // send data to client
      res.json(responseData)
  })
});

form.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="uft-8">
    <title>email form</title>
  </head>
  <body>
    <form action="/email_post" method="post">
      email : <input type="text" name="email"><br/>
      submit <input type="submit">
    </form>
    <button class="ajaxsend">ajaxsend</button>

    <div class="result"></div>

    <script>
      document.querySelector('.ajaxsend').addEventListener('click',function(){
        var inputdata = document.forms[0].elements[0].value;
        sendAjax('http://127.0.0.1:3000/ajax_send_email',inputdata);
      })

      function sendAjax(url, data){
        var data = {'email' : data};
        data = JSON.stringify(data);

        var xhr = new XMLHttpRequest();
        xhr.open('POST', url);
        xhr.setRequestHeader('Content-Type', "application/json");
        xhr.send(data);

        xhr.addEventListener('load', function(){
          var result = JSON.parse(xhr.responseText);
          var resultDiv = document.querySelector(".result");
          //if result.result is not ok, print "not Found"
          if(result.result !== "ok") resultDiv.innerHTML = "not found"
          //if result.result is ok, print result.name;
          else resultDiv.innerHTML = result.name;
          console.log(xhr.responseText);
        });
      }
    </script>
  </body>
</html>





© 2017. by isme2n

Powered by aiden