250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ์ปดํฌ์ง€์…˜
  • ใ…ฃใ„ท
  • MySQL
  • ์œ„์ž„

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

Javascript/Node.js

[Node.js] ๋ชจ๋“ˆ (export, require)

2022. 5. 15. 17:25
728x90

<์„ ์š”์•ฝ>

- package.json ์—   "type": "module", ์ถ”๊ฐ€ํ•ด์ค„ ๊ฒƒ

- export ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ

- import {๊ฐ€์ ธ์˜ฌ์ด๋ฆ„1, ๊ฐ€์ ธ์˜ฌ์ด๋ฆ„2 . . . } from '๊ฒฝ๋กœ์ง€์ •'

- ํ•œ๊บผ๋ฒˆ์— : import * as ๊ฐ€์ ธ์˜ฌ์ด๋ฆ„ from "๊ฒฝ๋กœ์ง€์ •"; ์จ์ค€๋‹ค์Œ์— ๊ฐ€์ ธ์˜ฌ์ด๋ฆ„.๊ฐ€์ ธ์˜ฌํ•จ์ˆ˜๋ช…() ์ด๋Ÿฐ์‹์œผ๋กœ ๋ถ€๋ฆ„

 

<์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ๋ฒ„์ „ ์ ์šฉ ์ „์˜ ๋ฐฉ์‹>

 

counter.js

let count = 0;
function increase() {
  return count++;
}

function getCount() {
  return count;
}

module.exports.getCount = getCount;
module.exports.increase = increase;

์œ„ ์ฝ”๋“œ์™€ ๋™์ผ ๊ธฐ๋Šฅ

let count = 0;
function increase() {
  return count++;
}

function getCount() {
  return count;
}

module.exports.getCount = getCount;
exports.increase = increase;

app.js

const counter = require("./counter.js");

counter.increase();
counter.increase();
counter.increase();
counter.increase();
counter.increase();
console.log(counter.getCount());

 

PS C:\Users\User\gitprogect\NodeJs> node app
5

 

<์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ๋ฒ„์ „ ์ ์šฉ ํ›„์˜ ๋ฐฉ์‹>

 

package.json

{
  "name": "nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

counter.js

let count = 0;
export function increase() {
  return count++;
}

export function getCount() {
  return count;
}

app.js

import { increase, getCount } from "./counter.js";

increase();
increase();
increase();
increase();
increase();
console.log(getCount());

ํ„ฐ๋ฏธ๋„ ๊ฒฐ๊ณผ

node app
5

 

ํ•œ๊บผ๋ฒˆ์— ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ๋‹ค๋ฉด * as ๋ณ„๋ช…์‹์œผ๋กœ ์จ์ค€๋‹ค.

import * as counter from "./counter.js";

counter.increase();
counter.increase();
counter.increase();
counter.increase();
counter.increase();
console.log(counter.getCount());

 


package.json

{
  //.. ์ƒ๋žต
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "dependencies": {
    "express": "^4.18.1"
  }
  //.. ์ƒ๋žต
}

 

index.js (package.json์—์„œ "type":"moduel"์ถ”๊ฐ€ ํ›„, ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์—ฌ๋„ ๊ดœ์ฐฎ๋‹ค)

//const express = require("express");
import express from "express";
const app = express();

 "type":"moduel" ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž์ฒด์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ์„ ์“ฐ๊ฒ ๋‹ค๋Š” ๋œป

์ผ€์ด์Šค1

index.js

const express = require("express"); //์„ค์น˜ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ฒจ๋ถ€
const app = express(); //์ฒจ๋ถ€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ์ฒด ์ƒ์„ฑ

//app.listen(ํŒŒ๋ผ๋ฏธํ„ฐ1, ํŒŒ๋ผ๋ฏธํ„ฐ2); ์ž์‹ ์˜ ์ปดํ“จํ„ฐ์— ์„œ๋ฒ„๋ฅผ ์—ด ์ˆ˜ ์žˆ๋‹ค.
//app.listen(์„œ๋ฒ„๋„์šธ ํฌํŠธ๋ฒˆํ˜ธ, ๋„์šด ํ›„ ์‹คํ–‰ํ•  ์ฝ”๋“œ);

app.listen(8080, function () {
  console.log("listening on 8080");
});

//8080 port๋กœ ์›น์„œ๋ฒ„๋ฅผ ์—ด๊ณ ,
//์ž˜ ์—ด๋ฆฌ๋ฉด console.log("listening on 8080");
// app.get('/bueaty', function(์š”์ฒญ, ์‘๋‹ต){
//     ์‘๋‹ต.send('๊พธ์ค€ํ•œ ์•„๋ฆ„๋‹ค์›€');
// })
// app.get('/pet', function(์š”์ฒญ,์‘๋‹ต){
//     ์‘๋‹ต.send('ํŽซ์šฉํ’ˆ ์‚ฌ์ดํŠธ');
// })

app.get("/pet", function (request, response) {
  response.send("ํŽซ์šฉํ’ˆ์„ ์‡ผํ•‘ํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค.");
});

์ผ€์ด์Šค 2

package.json

{
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.18.1"
  }
}

 

index.js

import express from "express";
const app = express();

app.listen(8800, () => {
  console.log("Conected to backend.");
});

 

ํ„ฐ๋ฏธ๋„

@1.0.0 start
> node index.js

Conected to backend.

728x90

'Javascript > Node.js' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Node.js] ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ํด๋” ์ด๋ฆ„ ์•ˆ์ „ํ•˜๊ฒŒ ๋ฐ›์•„์˜ค๊ธฐ  (0) 2022.08.23
[NodeJs] Get, Post  (0) 2022.05.24
[Node.js] ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„์˜ ๊ฐœ๋…, AWS ํด๋ผ์šฐ๋“œ ์„œ๋ฒ„ ํ™˜๊ฒฝ ๊ตฌ์ถ• ์‹ค์Šต  (0) 2022.05.14
[Node.js] ๋ฐฑ์—”๋“œ ๊ฐœ๋… ๊ธฐ์ดˆ ์˜์ƒ๋ชจ์Œ  (0) 2022.05.14
[node.js] ์ดˆ๊ฐ„๋‹จ ์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ (2) Node + Express ์›น์„œ๋ฒ„ ๊ธฐ๋ณธ์„ธํŒ…  (0) 2022.05.14
    'Javascript/Node.js' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [Node.js] ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ํด๋” ์ด๋ฆ„ ์•ˆ์ „ํ•˜๊ฒŒ ๋ฐ›์•„์˜ค๊ธฐ
    • [NodeJs] Get, Post
    • [Node.js] ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„์˜ ๊ฐœ๋…, AWS ํด๋ผ์šฐ๋“œ ์„œ๋ฒ„ ํ™˜๊ฒฝ ๊ตฌ์ถ• ์‹ค์Šต
    • [Node.js] ๋ฐฑ์—”๋“œ ๊ฐœ๋… ๊ธฐ์ดˆ ์˜์ƒ๋ชจ์Œ
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”