Javascript/Node.js

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

Rainbow๐ŸŒˆCoder 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