728x90
반응형
질문 : 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
728x90
반응형
'프로그래밍 언어 > HTML,CSS,JS' 카테고리의 다른 글
자바 스크립트에서 "=>" (화살표 기능) 의 의미 (0) | 2021.09.03 |
---|---|
JavaScript chop/slice/trim 문자열의 마지막 문자 잘라내기 (0) | 2021.09.02 |
JSLint "missing radix parameter" 오류 (0) | 2021.09.02 |
CSS를 사용하여 텍스트 또는 이미지에 투명한 배경을 제공하는 방법 (0) | 2021.09.02 |
CSS를 사용하여 height: 0 를 height: auto 로 전환하는 방법 (0) | 2021.09.01 |