###### tags: `jptw` `thesis` `technology` `nodejs` `webpage` # Notes for Node.js ## Folder Structure ``` ├── /node_modules ├── /views │ ├── index.html │ └── demo.html ├── /public │ ├── /assets │ ├── /css │ ├── /js │ └── /lib ├── server.js └── package.json ``` ## Setup Nodejs Server ```cmd // init $ npm init // install packages $ npm install express --save $ npm install ejs --save // run $ node server.js ``` ## Example ``` const express = require('express'); const app = express(); const path = require('path'); const router = express.Router(); const PORT = 8080; // View engine setup app.set('views', path.join(__dirname,'/views')); app.set("view options", {layout: false}); app.set('view engine', 'html'); app.engine('html', require('ejs').renderFile); // Router setup router.get('/',function(req,res){ res.render('index'); }); router.get('/demo',function(req,res){ res.render('demo'); }); app.use(express.static(__dirname + '/public'), router); app.listen(PORT, function() { console.log('Label Toolkit listening on port ' + PORT); }); ``` > **Ref**: > Express > [1] [有輪堪用直須用:Express.js](https://ithelp.ithome.com.tw/articles/10186877) > [2] [Node.js — 從一個實例看Express 的運作方式](https://seanyeh.medium.com/%E5%BE%9Enode-js-%E5%BE%9E%E4%B8%80%E5%80%8B%E5%AF%A6%E4%BE%8B%E7%9C%8Bexpress-%E7%9A%84%E9%81%8B%E4%BD%9C%E6%96%B9%E5%BC%8F-7c61cdd477f5) > [3] [4.x API | Express](http://expressjs.com/en/api.html) > [4] [How do I use HTML as the view engine in Express?](https://stackoverflow.com/questions/17911228/how-do-i-use-html-as-the-view-engine-in-express) > [5] [NodeJS + Express + i18next 支援多國語系吧!](https://fred-zone.blogspot.com/2012/01/nodejs-express-i18next.html) > > Other > [1] [Folder structure for a Node.js project](https://stackoverflow.com/questions/5178334/folder-structure-for-a-node-js-project) > [2] [1. 命名規則 | GitBook](https://events.storm.mg/codingstyle/coding_style/nodejs/naming_rules.html) > [3] [Node.js 是什麼?跟 JavaScript 有什麼關係](https://tw.alphacamp.co/blog/node-js-and-javascript) ## Environ #### `dotenv` ``` ** .env ** NODE_ENV=PRODUCTION DATABASE_HOST=localhost ``` ```javascript ** server.js ** require('dotenv').config(); console.log(process.env.NODE_ENV); ``` > Ref: > [1] [Github - motdotla / dotenv](https://github.com/motdotla/dotenv) > [2] [How can I set an environmental variable in node.js?](https://stackoverflow.com/questions/10829433/how-can-i-set-an-environmental-variable-in-node-js) > > Others: > [1] [Managing Front-end JavaScript Environment Variables](https://www.robertcooper.me/front-end-javascript-environment-variables) > [2] [dotenv-webpack](https://www.npmjs.com/package/dotenv-webpack) > [3] [pass value from node.js to javascript](https://stackoverflow.com/questions/51460745/pass-value-from-node-js-to-javascript)