- 전체
- HTML
- Web Design (웹디자인)
- XE 응용 개발
- wordpress plugin dev
- Javascript & JavaScript Application
- MEAN Stack : full stack javascript
- angular js & ionic framework
- bootstrap
- WebGL, Three.js and Babylon.js
- restful api design
- mobile web
- node.js 응용
- Cloud Service 응용
- 웹 어셈블리 개발 [WASM, WebAssembly]
- 마이크로서비스, MSA (microservice architecture)
- WebGL / WebGPU
- next.js 개발
- micro frontend (마이크로프론트앤드)
- 전자상거래/쇼핑몰
- 서버 클라우드 (aws, azure, google)
node.js 응용 [node.js 응용] Node.js에서 다른 파일의 함수를 "include" 하는 방법
2024.02.28 10:23
[node.js 응용] Node.js에서 다른 파일의 함수를 "include" 하는 방법
질문 : Node.js에서 다른 파일의 함수를 어떻게 "포함"합니까?
app.js라는 파일이 있다고 가정 해 보겠습니다. 매우 간단합니다.
|
|
var express = require('express'); |
|
|
var app = express.createServer(); |
|
|
app.set('views', __dirname + '/views'); |
|
|
app.set('view engine', 'ejs'); |
|
|
app.get('/', function(req, res){ |
|
|
res.render('index', {locals: { |
|
|
title: 'NowJS + Express Example' |
|
|
}}); |
|
|
}); |
|
|
|
|
|
app.listen(8080); |
"tools.js"에 함수가 있으면 어떻게됩니까? apps.js에서 사용하려면 어떻게 가져 오나요?
아니면 .. "도구"를 모듈로 바꾸고 그것을 요구해야합니까? << 힘들어 보이지만 tools.js 파일의 기본 가져 오기를 수행합니다.
답변
모든 js 파일을 요구할 수 있으며 노출하려는 것을 선언하기 만하면됩니다.
|
|
// tools.js |
|
|
// ======== |
|
|
module.exports = { |
|
|
foo: function () { |
|
|
// whatever |
|
|
}, |
|
|
bar: function () { |
|
|
// whatever |
|
|
} |
|
|
}; |
|
|
|
|
|
var zemba = function () { |
|
|
} |
그리고 앱 파일에서 :
|
|
// app.js |
|
|
// ====== |
|
|
var tools = require('./tools'); |
|
|
console.log(typeof tools.foo); // => 'function' |
|
|
console.log(typeof tools.bar); // => 'function' |
|
|
console.log(typeof tools.zemba); // => undefined |
출처 : https://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files
[출처] https://rateye.tistory.com/1017
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.

