본문 바로가기

728x90

기타/node js

(32)
nodejs axios get, post예제 axios는 node js와 클라이언트(크롬, 익스플로어 모두 지원)를 위한 http 통신 라이브러리입니다. 패키지 다운로드 방법은 npm install axios를 사용하면 할 수 있습니다. get과 post방식이 있습니다. 1.get방식 const axios = require('axios') axios({ method : 'get', url : 'https://www.naver.com/' }).then((res)=>{ console.log(res) }) 2.post방식 const axios = require('axios') axios.post('http://localhost:3000/axios',{ name : 'balmostory' }).then((res)=>{ console.log(res) }) ..
node js ajax post 예제 ajax는 페이지의 일부만 로드하는 기술입니다. 오늘은 nodejs ajax를 사용하는 방법에 대해 알아보도록 하겠습니다. 1. html 코드 작성 아래와 같이 html 코드를 작성해 주세요. ajaxbutton 2. 서버 코드 const express = require('express'); const app = express(); //post방식으로 데이터를 받을 때 필요한 모듈입니다. //req에 데이터를 담아줍니다. var bodyParser = require('body-parser') app.use(bodyParser.json()) app.use(bodyParser.urlencoded({extended:true})) //cors정책을 피하기 위한 모듈 const cors = require('cor..
[ Node js] nest js란? nestjs는 node js의 프레임워크 중 하나입니다. 아키텍처의 주요한 문제를 효율적으로 해결하는 것을 최우선 과제로 합니다. typescript를 지원하며 javascript로 작성하는 것도 가능합니다. nest js에서 제공하는 인터페이스 뿐 아니라 express, fastify의 api도 이용가능합니다. 즉 호환성이 좋습니다. 또한 객체지향 프로그래밍, 함수지향 프로그래밍, Functional Reactive 프로그래밍이 모두 가능합니다. nest js의 공식문서를 보면 angular에서 많은 영감을 받았다고 합니다. 프로젝트 생성하기 npm i -g @nestjs/cli nest new project-name 이렇게 간단하게 프로젝트를 생성할 수 있습니다.
[Node js] express 서버 예제 express는 nodejs로 빠르게 웹을 개발할 수 있도록 하는 프레임워크입니다. 자유롭게 사용할 수 있는 api들이 있습니다. 간단하게 사용방법에 대해 알아보도록 하겠습니다. 1.express모듈 설치하기. npm i express 2. 모듈 불러오기. var express = require('express') 3. 객체 생성하기. var app = express(); 4. 서버 만들기. var express = require('express') var app = express(); app.get('/home',(req,res)=>{ res.end('hi') }) app.listen(3000, () => { console.log('listening on 3000') }) locahttp://local..
[Node js] request 모듈 사용법 request은 url에 요청을 보내고 response를 받는 모듈입니다. 사용방법에 대해 알아보도록 하겠습니다. const request = require('request'); request('https://balmostory.tistory.com/', function (error, response, body) { console.error('error:', error); // Print the error if one occurred console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received console.log('body:', body); // ..
[Node js] chalk로 log colors 지정하기. chalk는 cmd에 나오는 log를 스타일링할 수 있는 패키지입니다. 'npm i chalk'를 사용해 다운로드해주시면 됩니다. 예시를 통해 알아보도록 하겠습니다. const chalk = require('chalk'); const log = console.log; log(chalk.blue('Hello') + ' balmostory' + chalk.red('!')); log(chalk.yellow.bgRed.bold('Hello balmostory!')); log(chalk.red('Hello', chalk.underline.bgBlue('balmostory') + '!')); chalk.color를 통해 색을 입힐 수 있습니다. bgcolor로 백그라운드 컬러도 줄 수 있고 underline을 통해..
[node js] lodash example lodash란 자바스크립트에서 array, collection, data, number, object 등을 이용해 데이터를 쉽게 다룰 수 있도록 도와주는 패키지입니다. npm에서 다운로드가 가장많은 패키지입니다. lodash의 기본 사용법에 대해 알아보도록 하겠습니다. 다운로드 방법은 간단합니다. cmd를 실행한 후 'npm i lodash'를 입력해주시면 됩니다. 1.import 방법.(nodejs 기준) // Load the full build. var _ = require('lodash'); // Load the FP build for immutable auto-curried iteratee-first data-last methods. var fp = require('lodash/fp'); // L..
[Node js] mysql 연동하기 nodejs에서 mysql(데이터 베이스)를 연동하기 위해서는 우선 mysql 패키지를 다운로드해주어야 합니다. npm install mysql 을 커맨드에서 실행해주세요. 1.mysql 모듈 가져오기. const mysql = require('mysql') 2.connection 생성하기.(host는 아마 바꾸시지 않아도 될 겁니다.) const connection = mysql.createConnection({ host: 'localhost', user: 'planneruser', password: '123456', database: 'plannerdb' }); 3.connection query생성하기. 'mysql문법을 입력하는 부분입니다.' connection.query('select * from..

728x90