Skip to content

Latest commit

Β 

History

History
126 lines (95 loc) Β· 2.57 KB

node-js02.md

File metadata and controls

126 lines (95 loc) Β· 2.57 KB

ν•œμ‹œκ°„λ§Œμ— λλ‚΄λŠ” Node.js μž…λ¬Έ

Node.js

  • JSλŠ” ν΄λΌμ΄μ–ΈνŠΈ ν”„λ‘œκ·Έλž˜λ° 언어인데 node.js둜 μ„œλ²„λ„ κ°€λŠ₯

μ—°μŠ΅

  • 이메일 λ°œμ†‘ ν”„λ‘œκ·Έλž¨ (nodemailer λͺ¨λ“ˆ)
const nodemailer = require('nodemailer');
const email = {
  host: 'smtp.mailtrap.io',
  port: 2525,
  auth: {
    user: '1c6525bec23ec2',
    pass: 'a08f059bf556a2',
  },
};

const send = async (option) => {
  nodemailer.createTransport(email).sendMail(option, (error, info) => {
    if (error) console.log(error);
    else {
      console.log(info);
      return info.response;
    }
  });
};

let emailData = {
  from: 'pangoons@gmail.com',
  to: 'pangoons@gmail.com',
  subject: 'ν…ŒμŠ€νŠΈ 메일 μž…λ‹ˆλ‹€.',
  text: 'nodejs ν•œμ‹œκ°„λ§Œμ— λλ‚΄λ³΄μž',
};

send(emailData);
  • μ›Ήμ„œλ²„ λ§Œλ“€κΈ° μ‹€μŠ΅ (express λͺ¨λ“ˆ)
const express = require('express');
const app = express();

const server = app.listen(3000, () => {
  console.log('Start Server : localhost:3000');
});

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.get('/', function (req, res) {
  res.render('index.html');
});

app.get('/about', function (req, res) {
  res.render('about.html');
});
  • db 연동 μ‹€μŠ΅
const express = require('express');
const app = express();

const server = app.listen(3000, () => {
  console.log('Start Server : localhost:3000');
});

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.get('/', function (req, res) {
  res.render('index.html');
});

app.get('/about', function (req, res) {
  res.render('about.html');
});

var mysql = require('mysql');
var pool = mysql.createPool({
  host: '',
  user: '',
  port: '',
  password: '',
  database: '',
});

app.get('/db', function (req, res) {
  pool.getConnection(function (err, connection) {
    if (err) throw err; // not connected!

    // Use the connection
    connection.query('SELECT * from Board', function (error, results, fields) {
      res.send(JSON.stringify(results));
      console.log('results', results);

      // When done with the connection, release it.
      connection.release();

      // Handle error after the release.
      if (error) throw error;

      // Don't use the connection here, it has been returned to the pool.
    });
  });
});

[Note]

  • μ›λž˜λŠ” ν΄λΌμ΄μ–ΈνŠΈ ν”„λ‘œκ·Έλž˜λ° 언어와 μ„œλ²„ ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄κ°€ 닀름
    => ν•˜μ§€λ§Œ JS만 μ•Œλ©΄ λͺ¨λ‘ κ°€λŠ₯!

Reference