<์ ์์ฝ>
- 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.
'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 |