개발관련 글입니다.!! 공부 하는즁!!
아시다시피 ES6와 Node.js는 문법이 조금 다릅니다.
그러다보니 항상 헷갈리네요..!!
그래서 정리한판..!!
NodeJS
- 여러방법이 있겠지만, 이러한 방법도 있습니다.
- 기본적으로 폴더내의 index.js를 먼저 읽으므로 index.js에 아래와 같이 코딩합니다.
- 그러면 해당 폴더내에 존재하는 다른 js 파일들의 module.exports 된 내용들을 가져오게 합니다.
- exports 방법에 따라서 다양하게 활용은 가능합니다.
// index.js
var normalizedPath = require("path").join(__dirname, "./");
const modules = {};
require("fs")
.readdirSync(normalizedPath)
.forEach(function (file) {
if (file.indexOf("index.js") > -1) return;
const moduleName = file.replace(/(\.\/|\.js)/g, "");
modules[moduleName] = require("./" + file);
});
module.exports = modules;
- 다른모듈 샘플
// test.js
module.exports = {
name:"TEST",
val:15
}
- 방법2
- glob를 사용하는 방법입니다.
- 이 방법은 실행경로에 따라서 경로설정을 주의해야합니다.
- 한번 해보시면서 경로에러가 뜨면 조금씩 수정하면 됩니다.
var glob = require( 'glob' )
, path = require( 'path' );
glob.sync( './routes/**/*.js' ).forEach( function( file ) {
require( path.resolve( file ) );
});
ES6
- ES6는 import와 export default 를 이용하여 아래와 같이 설정을 하면 됩니다.
- 다른 js 모듈에서
export default
를 사용한다는 가정하에 사용하는 방법입니다.
// index.js
const requireModule = require.context('.', false, /\.js$/);
const modules = {};
requireModule.keys().forEach(fileName => {
if (fileName === './index.js') return;
const moduleName =fileName.replace(/(\.\/|\.js)/g, '');
modules[moduleName] = requireModule(fileName).default;
});
export default modules;
- 다른 모듈 샘플
import Vue from 'vue';
const state = {
username: null,
account: null,
};
const mutations = {
saveUsername(_state, payload) {
Vue.set(_state, 'username', payload);
},
saveAccount(_state, payload) {
Vue.set(_state, 'account', payload);
},
logout(_state) {
Vue.set(_state, 'username', null);
},
};
const actions = {
logout: () => {
window.location = '/';
},
};
export default {
state,
mutations,
actions,
};
제가 가장 필요해서 정리해봅니다!!
참고바래봅니다.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
앗! 또 이상한걸... ㅋㅋ 맛점 하시고, 즐거운 오후 되세요^^
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
독거형님이 파워다운 하셨다고 하니.. 갑자기 안보일까바 무섭습니당 ㅠㅠ
독거형님...!!! 언능 잘되셔서 다시 글로 나타나주셔요!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
ㅎㅎ 댓글도 계속 달꺼고.... 뻘짓, 독거프로젝트도 계속 포스팅 할껍니다^^
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
오호 모듈을 하나씩 말고 경로로 설정할 수 있는건가요? ㅎㅎ
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
넵! 위 방법을 통하면 파일 추가가 될 때마다 Module이 추가가 되는 방식이지요!!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@happyberrysboy transfered 50 KRWP to @krwp.burn. voting percent : 76.74%, voting power : 48.91%, steem power : 1879413.31, STU KRW : 1200.
@happyberrysboy staking status : 13840 KRWP
@happyberrysboy limit for KRWP voting service : 13.84 KRWP (rate : 0.001)
What you sent : 50 KRWP
Refund balance : 36.16 KRWP [59339141 - 2f34c307ea01fa9dac420950ded11759993e2ba6]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit