การสาธิตสด : ลิงค์
ข้ามไปที่มีอะไรใหม่?
แอปพลิเคชันเว็บหม้อต้มสำหรับ Node.js
หากคุณเคยเข้าร่วมแฮ็คฮอร์ในอดีตคุณจะรู้ว่าต้องใช้เวลานานเท่าใดในการเริ่มต้นโครงการ: ตัดสินใจว่าจะสร้างอะไรเลือกภาษาการเขียนโปรแกรมเลือกเฟรมเวิร์กเว็บเลือกกรอบ CSS ในอีกไม่นานคุณอาจมีโครงการเริ่มต้นใน GitHub และจากนั้นสมาชิกในทีมคนอื่น ๆ ก็สามารถเริ่มมีส่วนร่วมได้ หรือวิธีการเกี่ยวกับการทำอะไรง่ายๆเหมือน ลงชื่อเข้าใช้การตรวจสอบสิทธิ์ Facebook ? คุณสามารถใช้เวลาหลายชั่วโมงหากคุณไม่คุ้นเคยกับวิธีการทำงานของ OAuth 2.0
เมื่อฉันเริ่มโครงการนี้จุดสนใจหลักของฉันคือ ความเรียบง่าย และ ใช้งานง่าย ฉันยังพยายามทำให้มันเป็น เรื่องทั่วไป และ สามารถนำกลับมาใช้ใหม่ได้ มากที่สุดเพื่อครอบคลุมกรณีการใช้งานส่วนใหญ่ของแอป Hackathon Web โดยไม่เฉพาะเจาะจงเกินไป ในกรณีที่เลวร้ายที่สุดคุณสามารถใช้สิ่งนี้เป็นคู่มือการเรียนรู้สำหรับโครงการของคุณหากตัวอย่างเช่นคุณสนใจที่จะ ลงชื่อเข้าใช้กับ Google Authentication และไม่มีอะไรอื่น
“ ดีมาก!
- Adrian Le Bas
“ สุดยอดเยี่ยมมาก”
- Steven Rueter
“ ฉันใช้มันมาหนึ่งปีแล้วและหลายโครงการมันเป็นแผ่นหม้อไอน้ำที่ยอดเยี่ยมและโครงการได้รับการดูแลอย่างดี!”
- Kevin Granger
“ โลกเล็ก ๆ ที่มีโครงการของ Sahat เราใช้ Hackathon Starter ของเขาสำหรับ Hackathon ของเราเมื่อสุดสัปดาห์ที่ผ่านมาและได้รับรางวัล repo ที่มีประโยชน์จริงๆ!”
- สัมภาษณ์ผู้สมัครสำหรับหนึ่งใน บริษัท ที่ฉันเคยทำงานด้วย
MongoDB (ติดตั้งในท้องถิ่นหรือโฮสต์)
Node.js 18+
เครื่องมือบรรทัดคำสั่ง
Mac OS X: XCode (หรือ OS X 10.9+ : xcode-select --install )
Windows: Visual Studio Code + ระบบย่อย Windows สำหรับ Linux - Ubuntu หรือ Visual Studio
Ubuntu / Linux Mint: sudo apt-get install build-essential
Fedora : sudo dnf groupinstall "Development Tools"
OpenSuse: sudo zypper install --type pattern devel_basis
หมายเหตุ: หากคุณยังใหม่ต่อโหนดหรือ Express คุณอาจพบ Node.js & Express จาก Scratch Series ที่เป็นประโยชน์สำหรับการเรียนรู้พื้นฐานของ Node และ Express อีกทางเลือกหนึ่งนี่คือบทช่วยสอนที่ยอดเยี่ยมสำหรับผู้เริ่มต้นที่สมบูรณ์ - เริ่มต้นด้วย node.js, Express, MongoDB
ขั้นตอนที่ 1: วิธีที่ง่ายที่สุดในการเริ่มต้นคือการโคลนที่เก็บ:
# Get the latest snapshot
git clone https://github.com/sahat/hackathon-starter.git myproject
# Change directory
cd myproject
# Install NPM dependencies
npm install
# Then simply start your app
node app.js หมายเหตุ: ฉันขอแนะนำให้ติดตั้ง Nodemon มันดูการเปลี่ยนแปลงใด ๆ ในแอป Node.js ของคุณและรีสตาร์ทเซิร์ฟเวอร์โดยอัตโนมัติ เมื่อติดตั้งแล้วแทนที่จะใช้ node app.js ใช้ nodemon app.js มันจะช่วยให้คุณประหยัดเวลาได้มากในระยะยาวเพราะคุณไม่จำเป็นต้องรีสตาร์ทเซิร์ฟเวอร์ด้วยตนเองทุกครั้งที่คุณเปลี่ยนรหัสเล็กน้อย ในการติดตั้งให้เรียกใช้ sudo npm install -g nodemon
ขั้นตอนที่ 2: รับคีย์ API และเปลี่ยนการกำหนดค่าหากจำเป็นหลังจากเสร็จสิ้นขั้นตอนที่ 1 และติดตั้ง MongoDB ในเครื่องคุณควรเข้าถึงแอปพลิเคชันผ่านเว็บเบราว์เซอร์และใช้บัญชีผู้ใช้ในท้องถิ่น อย่างไรก็ตามฟังก์ชั่นบางอย่างเช่นการรวม API อาจทำงานได้อย่างถูกต้องจนกว่าคุณจะได้รับคีย์เฉพาะจากผู้ให้บริการ กุญแจที่มีให้ในโครงการทำหน้าที่เป็นตัวยึดตำแหน่งและคุณสามารถเก็บไว้สำหรับคุณสมบัติที่คุณไม่ได้ใช้ในปัจจุบัน ในการรวมปุ่มที่ได้มาไว้ในแอปพลิเคชันคุณมีสองตัวเลือก:
export เช่นนี้: export FACEBOOK_SECRET=xxxxxx วิธีนี้ถือเป็นแนวปฏิบัติที่ดีกว่าเนื่องจากจะช่วยลดความเสี่ยงของการรวมความลับของคุณไว้ในที่เก็บรหัส.env.example : เปิดไฟล์ .env.example และอัปเดตคีย์ตัวยึดด้วยรหัสที่ได้มาใหม่ วิธีนี้มีความเสี่ยงในการตรวจสอบความลับของคุณในการเขียนรหัส reposสิ่งที่จะได้รับและกำหนดค่า:
SMTP
recaptcha
Oauth สำหรับการเข้าสู่ระบบโซเชียล (ลงชื่อเข้าใช้ / เข้าสู่ระบบด้วย)
คีย์ API สำหรับผู้ให้บริการในตัวอย่าง API หากคุณวางแผนที่จะใช้
MongoDB Atlas
ที่อยู่อีเมล
NGROK และ HTTPS หากคุณต้องการใช้ API บางส่วนที่ต้องการ HTTPS ในการทำงาน (เช่น Pinterest หรือ Facebook) คุณจะต้องดาวน์โหลด NGROK เริ่ม Ngrok ตั้งค่า base_url ของคุณเป็นที่อยู่ส่งต่อ (เช่น https://3ccb-1234-abcd.ngrok-free.app ) และใช้ที่อยู่ส่งต่อเพื่อเข้าถึงแอปพลิเคชันของคุณ หากคุณใช้พร็อกซีอย่าง NGROK คุณอาจได้รับข้อผิดพลาดที่ไม่ตรงกัน CSRF หากคุณพยายามเข้าถึงแอพที่ http://localhost:8080 แทนที่อยู่ https: //...ngrok-free.app
หลังจากติดตั้งหรือดาวน์โหลดไคลเอนต์ NGROK แบบสแตนด์อโลนคุณสามารถเริ่มต้น NGROK เพื่อสกัดกั้นข้อมูลที่แลกเปลี่ยนบนพอร์ต 8080 ด้วย ./ngrok http 8080 ใน Linux หรือ ngrok http 8080 ใน Windows
ขั้นตอนที่ 3: พัฒนาแอปพลิเคชันของคุณและปรับแต่งประสบการณ์
ขั้นตอนที่ 4: ตัวเลือก - ปรับใช้กับการผลิตดู:
คุณจะต้องได้รับข้อมูลรับรองที่เหมาะสม (รหัสลูกค้าความลับของลูกค้าคีย์ API หรือชื่อผู้ใช้และรหัสผ่าน) สำหรับ API และบริการที่คุณต้องการ ดูขั้นตอนที่ 2 ในส่วนการเริ่มต้นสำหรับข้อมูลเพิ่มเติม
รับข้อมูลรับรอง SMTP จากผู้ให้บริการสำหรับอีเมลธุรกรรม ตั้งค่า SMTP_USER, SMTP_Password และตัวแปรสภาพแวดล้อม SMTP_HOST ตามลำดับ เมื่อเลือกโฮสต์ SMTP โปรดทราบว่าแอพได้รับการกำหนดค่าให้ใช้การส่งสัญญาณ SMTP ที่ปลอดภัยผ่านพอร์ต 465 ออกจากกล่อง คุณมีความยืดหยุ่นในการเลือกผู้ให้บริการใด ๆ ที่เหมาะสมกับความต้องการของคุณหรือใช้ประโยชน์จากหนึ่งในผู้ให้บริการต่อไปนี้แต่ละคนเสนอระดับฟรีเพื่อความสะดวกของคุณ
| ผู้ให้บริการ | ชั้นฟรี | เว็บไซต์ |
|---|---|---|
| SendGrid | 100 อีเมล/วันฟรี | https://sendgrid.com |
| smtp2go | 1,000 อีเมล/เดือนฟรี | https://www.smtp2go.com |
| เบรโว | 300 อีเมล/วันฟรี | https://www.brevo.com |
.env คีย์เหล่านี้จะสามารถเข้าถึงได้ภายใต้การตั้งค่าปุ่ม Recaptcha แบบเลื่อนลงหากคุณต้องการอีกครั้งในภายหลังhttp://localhost:8080 ฯลฯ )http://localhost:8080/auth/google/callback ).envhttp://localhost:8080/auth/snapchat/callback ).env.env.env.envlocalhost ภายใต้ โดเมนแอพhttp://localhost:8080 ฯลฯ ) ภายใต้ URL ของไซต์http://localhost:8080/auth/facebook/callback ) ภายใต้การเปลี่ยนเส้นทาง OAuth ที่ถูกต้อง หมายเหตุ: หลังจากการลงชื่อเข้าใช้ที่ประสบความสำเร็จกับ Facebook ผู้ใช้จะถูกเปลี่ยนเส้นทางกลับไปที่หน้าแรกด้วยแฮชต่อท้าย #_=_ ใน URL มัน ไม่ใช่ ข้อผิดพลาด ดูการอภิปรายสแต็กล้นสำหรับวิธีจัดการกับมัน
http://localhost:8080 ฯลฯ ) เป็น URL หน้าแรกhttp://localhost:8080/auth/github/callback ).envhttp://localhost:8080 , ฯลฯ )http://localhost:8080/auth/twitter/callback ).envhttp://localhost:8080/auth/linkedin/callback )http://localhost:8080 , ฯลฯ )r_basicprofile.env.env.envhttp://localhost:8080/auth/foursquare/callback ).envhttp://localhost:8080/auth/tumblr/callback ).env.envhttp://localhost:8080/auth/twitch/callback ).env.env.env.env ของคุณ | ชื่อ | คำอธิบาย |
|---|---|
| config /passport.js | Passport Local Local และ Oauth กลยุทธ์รวมถึงมิดเดิลแวร์เข้าสู่ระบบ |
| คอนโทรลเลอร์ /api.js | คอนโทรลเลอร์สำหรับเส้นทาง /API และตัวอย่าง API ทั้งหมด |
| คอนโทรลเลอร์ /Contact.js | คอนโทรลเลอร์สำหรับแบบฟอร์มการติดต่อ |
| คอนโทรลเลอร์ /home.js | คอนโทรลเลอร์สำหรับโฮมเพจ (ดัชนี) |
| คอนโทรลเลอร์ /user.js | คอนโทรลเลอร์สำหรับการจัดการบัญชีผู้ใช้ |
| Models /user.js | สคีมาและรุ่น Mongoose สำหรับผู้ใช้ |
| สาธารณะ / | สินทรัพย์คงที่ (ฟอนต์, CSS, JS, IMG) |
| สาธารณะ / js /application.js | ระบุการพึ่งพา JavaScript ฝั่งไคลเอ็นต์ |
| สาธารณะ / js /app.js | วางจาวาสคริปต์ฝั่งไคลเอ็นต์ของคุณที่นี่ |
| สาธารณะ / css /main.scss | สไตล์ชีทหลักสำหรับแอปของคุณ |
| มุมมอง /บัญชี / | เทมเพลตสำหรับ การเข้าสู่ระบบรีเซ็ตรหัสผ่านการลงทะเบียนโปรไฟล์ |
| Views /API / | เทมเพลตสำหรับตัวอย่าง API |
| Views /Partials /flash.pug | ข้อผิดพลาดข้อมูลและการแจ้งเตือนแฟลชที่ประสบความสำเร็จ |
| Views /Partials /Heade.pug | เทมเพลตบางส่วนของ Navbar |
| Views /Partials /footer.pug | เทมเพลตส่วนท้ายบางส่วน |
| views /layout.pug | เทมเพลตฐาน |
| Views /home.pug | แม่แบบโฮมเพจ |
| .dockerignore | โฟลเดอร์และไฟล์ที่ถูกละเว้นโดยการใช้ Docker |
| . env.example | คีย์ API โทเค็นรหัสผ่านและฐานข้อมูล URI ของคุณ |
| .eslintrc | กฎสำหรับ Eslint Linter |
| . gitignore | โฟลเดอร์และไฟล์ถูกละเว้นโดย Git |
| app.js | ไฟล์แอปพลิเคชันหลัก |
| Docker-compose.yml | ไฟล์กำหนดค่า Docker เขียน |
| Dockerfile | ไฟล์กำหนดค่า Docker |
| package.json | การพึ่งพา NPM |
| package-lock.json | มีรุ่นที่แน่นอนของการพึ่งพา NPM ใน package.json |
หมายเหตุ: ไม่มีการตั้งค่าสำหรับวิธีการตั้งชื่อหรือโครงสร้างมุมมองของคุณ คุณสามารถวางแม่แบบทั้งหมดของคุณในไดเรกทอรี views ระดับบนสุดโดยไม่ต้องมีโครงสร้างโฟลเดอร์ซ้อนกันหากทำให้สิ่งต่าง ๆ ง่ายขึ้นสำหรับคุณ อย่าลืมอัปเดต extends ../layout และเส้นทาง res.render() ที่สอดคล้องกันในคอนโทรลเลอร์
| บรรจุุภัณฑ์ | คำอธิบาย |
|---|---|
| @fortawesome/fontawesome ฟรี | สัญลักษณ์และไอคอนไลบรารี |
| @googleapis/ไดรฟ์ | Google Drive API Integration Library |
| @googleapis/แผ่น | Google Sheets API Integration Library |
| @ladjs/bootstrap-social | ห้องสมุดปุ่มโซเชียล |
| @lob/lob-typescript-sdk | ห้องสมุด LOB (USPS MAILING / SERVICE SERVICE) |
| @node-rs/bcrypt | ไลบรารีสำหรับการแฮชและผ่านรหัสผ่านผู้ใช้ |
| @octokit/พักผ่อน | ห้องสมุด GitHub API |
| @Passport-JS/Passport-Twitter | การสนับสนุนการเข้าสู่ระบบ X (Twitter) (OAuth 2) |
| @popperjs/core | Frontend JS Library สำหรับ poppers และคำแนะนำเครื่องมือ |
| ซอกซอก | ไคลเอนต์ http |
| ผู้มีร่างกาย | Node.js Middleware Body Parsing |
| รองเท้าบู๊ต | กรอบ CSS |
| ไค | ห้องสมุดการยืนยัน BDD/TDD |
| ไชโย | หน้าเว็บขูดโดยใช้ไวยากรณ์สไตล์ jQuery |
| การบีบอัด | Node.js มิดเดิลแวร์การบีบอัด |
| Connect-Mongo | MongoDB Session Store สำหรับ Express |
| dotenv | โหลดตัวแปรสภาพแวดล้อมจากไฟล์. ENV |
| errorhandler | มิดเดิลแวร์ Handler ข้อผิดพลาดอย่างเดียวการพัฒนา |
| eslint | Linter JavaScript |
| ESLINT-CONFIG-AIRBNB-BASE | การกำหนดค่า ESLINT โดย Airbnb |
| Eslint-plugin-chai เป็นมิตร | ทำให้ eslint เป็นมิตรกับ chai.js 'คาดหวัง' และ 'ควร' ข้อความ |
| ESLINT-PLUGIN-IMPORT | ปลั๊กอิน ESLINT พร้อมกฎที่ช่วยตรวจสอบการนำเข้าที่เหมาะสม |
| ด่วน | Node.js Framework Web |
| ฟลาช | ให้ข้อความแฟลชสำหรับ Express |
| ค่า จำกัด | อัตราการ จำกัด มิดเดิลแวร์สำหรับการป้องกันการละเมิด |
| การแสดงความคิดเห็น | มิดเดิลแวร์เซสชันที่เรียบง่ายสำหรับ Express |
| แหบห้าว | Git Hook Manager เพื่อทำงานอัตโนมัติด้วย GIT |
| jQuery | Front-end JS Library เพื่อโต้ตอบกับองค์ประกอบ HTML |
| lastfm | Last.fm API Library |
| ระยะผ้าสำลี | ยูทิลิตี้ไปยังไฟล์ผ้าสำลีจัดแสดงโดย Git |
| ลูกเท | LOB API Library |
| การอึกทึก | ห้องสมุดยูทิลิตี้สำหรับการทำงานกับอาร์เรย์ตัวเลขวัตถุสตริง |
| Lusca | มิดเดิลแวร์ CSRF |
| MailChecker | ตรวจสอบว่าที่อยู่อีเมลนั้นถูกต้องและไม่ใช่ที่อยู่ที่ใช้แล้วทิ้ง |
| มอคค่า | กรอบทดสอบ |
| ช่วงเวลา | แยกวิเคราะห์ตรวจสอบวันที่คำนวณและเวลา |
| Mongodbmemoryserver | MongoDB ในหน่วยความจำ (สำหรับการทดสอบโดยไม่ต้องทำงาน DB) |
| พังพอน | MongoDB ODM |
| มอร์แกน | HTTP Request Logger Middleware สำหรับ Node.js. |
| ของ Multer | Node.js มิดเดิลแวร์สำหรับการจัดการ multipart/form-data |
| คนพยักหน้า | ไลบรารี Node.js สำหรับการส่งอีเมล |
| นิวยอร์ค | การทดสอบความครอบคลุม |
| หนังสือเดินทาง | ไลบรารีการตรวจสอบความถูกต้องที่เรียบง่ายและสง่างามสำหรับ Node.js |
| หนังสือเดินทาง | ลงชื่อเข้าใช้ด้วยปลั๊กอิน Facebook |
| Passport-Github2 | ลงชื่อเข้าใช้ด้วยปลั๊กอิน GitHub |
| Passport-google-oauth | ลงชื่อเข้าใช้กับ Google ปลั๊กอิน |
| Passport-LinkedIn-Outh2 | ลงชื่อเข้าใช้ด้วยปลั๊กอิน LinkedIn |
| หนังสือเดินทางท้องถิ่น | ลงชื่อเข้าใช้ด้วยปลั๊กอินชื่อผู้ใช้และรหัสผ่าน |
| หนังสือเดินทาง | ช่วยให้คุณตั้งค่ากลยุทธ์ OAuth 1.0a และ Oauth 2.0 ของคุณเอง |
| Passport-Ouuth2-refresh | ห้องสมุดเพื่อรีเฟรชโทเค็นการเข้าถึง OAuth 2.0 โดยใช้โทเค็นรีเฟรช |
| หนังสือเดินทาง | ลงชื่อเข้าใช้ด้วยปลั๊กอิน Snapchat |
| Passport-Steam-Openid | ปลั๊กอิน OpenID 2.0 Steam |
| แพทช์แพทช์แพทช์ | แก้ไขโมดูลโหนดที่เสียก่อนการแก้ไขโดยผู้ดูแล |
| PayPal-Rest-SDK | ห้องสมุด APIS PayPal |
| ตัก | เทมเพลตเอ็นจิ้นสำหรับ Express |
| เขี้ยว | Sass Compiler เพื่อสร้าง CSS ด้วยมหาอำนาจ |
| ไซอน | ทดสอบสายลับ, ต้นขั้วและการเยาะเย้ยสำหรับ JavaScript |
| แถบ | ไลบรารี API Stripe Offical |
| สุดยอด | ห้องสมุดการยืนยัน http |
| ทวิตเตอร์ | ห้องสมุด Twilio API |
| Twitch-Passport | ลงชื่อเข้าใช้ด้วยปลั๊กอิน Twitch |
| ผู้ตรวจสอบความถูกต้อง | ไลบรารีของตัวตรวจสอบความถูกต้องของสตริงและ sanitizers |
filesize(265318); // "265.32 kB"var token = _.find(req.user.tokens, { kind: 'twitter' }); โดยที่พารามิเตอร์ที่ 1 คืออาร์เรย์และพารามิเตอร์ที่ 2 เป็นวัตถุที่จะค้นหา 403 Error: Forbidden เมื่อส่งแบบฟอร์ม?คุณต้องเพิ่มองค์ประกอบอินพุตที่ซ่อนอยู่ต่อไปนี้ลงในแบบฟอร์มของคุณ สิ่งนี้ได้รับการเพิ่มในคำขอดึง #40 ซึ่งเป็นส่วนหนึ่งของการป้องกัน CSRF
input(type='hidden', name='_csrf', value=_csrf)
หมายเหตุ: ตอนนี้เป็นไปได้ที่จะอนุญาต URL บางอย่าง กล่าวอีกนัยหนึ่งคุณสามารถระบุรายการเส้นทางที่ควรข้ามการตรวจสอบการตรวจสอบ CSRF
หมายเหตุ 2: ในการอนุญาต URL แบบไดนามิกของผู้ใช้งาน Whitelist ใช้การทดสอบนิพจน์ทั่วไปภายในมิดเดิลแวร์ CSRF เพื่อดูว่า req.originalUrl ตรงกับรูปแบบที่คุณต้องการหรือไม่
นั่นคือข้อความแสดงข้อผิดพลาดที่กำหนดเองที่กำหนดไว้ใน app.js เพื่อระบุว่ามีปัญหาในการเชื่อมต่อกับ MongoDB:
mongoose . connection . on ( 'error' , ( err ) => {
console . error ( err ) ;
console . log ( '%s MongoDB connection error. Please make sure MongoDB is running.' , chalk . red ( '✗' ) ) ;
process . exit ( ) ;
} ) ; คุณต้องให้เซิร์ฟเวอร์ MongoDB ทำงานก่อนที่จะเปิดใช้ app.js คุณสามารถดาวน์โหลด MongoDB ได้ที่นี่หรือติดตั้งผ่านตัวจัดการแพ็คเกจ ผู้ใช้ Windows อ่านติดตั้ง MongoDB บน Windows
เคล็ดลับ: หากคุณเชื่อมต่อกับอินเทอร์เน็ตเสมอคุณสามารถใช้ MongoDB Atlas แทนการดาวน์โหลดและติดตั้ง MongoDB ในพื้นที่ คุณจะต้องอัปเดตข้อมูลรับรองฐานข้อมูลในไฟล์ .env เท่านั้น
โอกาสที่คุณไม่ได้เปลี่ยน ฐานข้อมูล URI ใน. .env หาก MONGODB ถูกตั้งค่าเป็น localhost มันจะทำงานบนเครื่องของคุณได้ตราบใดที่ MongoDB กำลังทำงานอยู่ เมื่อคุณปรับใช้เพื่อแสดงผล OpenShift หรือผู้ให้บริการรายอื่นคุณจะไม่มี MongoDB ทำงานบน localhost คุณต้องสร้างบัญชีด้วย MongoDB Atlas จากนั้นสร้างฐานข้อมูลระดับฟรี ดูการปรับใช้สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการตั้งค่าบัญชีและฐานข้อมูลใหม่ทีละขั้นตอนกับ MongoDB Atlas
เพื่อความเรียบง่าย ในขณะที่อาจมีวิธีการที่ดีกว่าเช่นการส่งบริบท app ไปยังคอนโทรลเลอร์แต่ละตัวตามที่ระบุไว้ในบล็อกนี้ฉันพบว่าสไตล์ดังกล่าวทำให้เกิดความสับสนสำหรับผู้เริ่มต้น ฉันใช้เวลานานในการเข้าใจแนวคิดของ exports และ module.exports ให้มีการอ้างอิง app ทั่วโลกในไฟล์อื่น ๆ ท่าสำหรับฉันคือการคิดย้อนหลัง app.js เป็น "หัวใจของแอพ" ควรเป็นรุ่นอ้างอิงเส้นทางเส้นทางคอนโทรลเลอร์ ฯลฯ เมื่อทำงานเดี่ยวในโครงการขนาดเล็กฉันชอบที่จะมีทุกอย่างใน app.js เช่นเดียวกับเซิร์ฟเวอร์ REST API นี้
ส่วนนี้มีไว้เพื่อให้คำอธิบายโดยละเอียดเกี่ยวกับวิธีการทำงานของฟังก์ชั่นเฉพาะ บางทีคุณอาจอยากรู้ว่ามันทำงานอย่างไรหรือบางทีคุณอาจหลงทางและสับสนในขณะที่อ่านรหัสฉันหวังว่ามันจะให้คำแนะนำแก่คุณ
HTML5 UP มีเทมเพลตที่สวยงามมากมายที่คุณสามารถดาวน์โหลดได้ฟรี
เมื่อคุณดาวน์โหลดไฟล์ zip มันจะมาพร้อมกับ index.html , รูปภาพ , โฟลเดอร์ CSS และ JS ดังนั้นคุณจะรวมเข้ากับ Hackathon Starter ได้อย่างไร? Hackathon Starter ใช้เฟรมเวิร์ก Bootstrap CSS แต่เทมเพลตเหล่านี้ไม่ได้ การพยายามใช้ไฟล์ CSS ทั้งสองในเวลาเดียวกันอาจส่งผลให้เกิดเอฟเฟกต์ที่ไม่พึงประสงค์
หมายเหตุ: การใช้วิธีเทมเพลตที่กำหนดเองคุณควรเข้าใจว่าคุณไม่สามารถนำมุมมองใด ๆ ที่ฉันสร้างขึ้นมาใหม่ได้: เลย์เอาต์, โฮมเพจ, เบราว์เซอร์ API, เข้าสู่ระบบ, การลงทะเบียน, การจัดการบัญชี, การติดต่อ มุมมองเหล่านั้นถูกสร้างขึ้นโดยใช้กริด bootstrap และสไตล์ คุณจะต้องอัปเดตกริดด้วยตนเองโดยใช้ไวยากรณ์อื่นที่มีให้ในเทมเพลต ต้องบอกว่าคุณสามารถผสมและจับคู่ได้หากคุณต้องการทำเช่นนั้น: ใช้ bootstrap สำหรับอินเทอร์เฟซแอพหลักและเทมเพลตที่กำหนดเองสำหรับหน้า Landing Page
เริ่มต้นตั้งแต่ต้น สำหรับตัวอย่างนี้ฉันจะใช้เทมเพลต Escape Velocity:
หมายเหตุ: เพื่อความเรียบง่ายฉันจะพิจารณาเฉพาะ index.html และข้าม left-sidebar.html , no-sidebar.html , right-sidebar.html
ย้ายไฟล์ JavaScript ทั้งหมดจาก html5up-escape-velocity/js ไปยัง public/js จากนั้นย้ายไฟล์ CSS ทั้งหมดจาก html5up-escape-velocity/css ไปยัง public/css และในที่สุดก็ย้ายรูปภาพทั้งหมดจาก html5up-escape-velocity/images ไปยัง public/images คุณสามารถย้ายไปยังโฟลเดอร์ IMG ที่มีอยู่ได้ แต่นั่นจะต้องเปลี่ยนการอ้างอิง img ทุกครั้งด้วยตนเอง คว้าเนื้อหาของ index.html และวางลงใน html เพื่อ pug
หมายเหตุ: อย่าลืมอัปเดตเส้นทาง CSS และ JS ทั้งหมดตามลำดับ
สร้างไฟล์ escape-velocity.pug ใหม่และวางมาร์กอัปปั๊กในโฟลเดอร์ views เมื่อใดก็ตามที่คุณเห็นรหัส res.render('account/login') - นั่นหมายความว่าจะค้นหาไฟล์ views/account/login.pug
มาดูกันว่ามันมีลักษณะอย่างไร สร้างคอนโทรลเลอร์ใหม่ escapevelocity ภายใน controllers/home.js :
exports . escapeVelocity = ( req , res ) => {
res . render ( 'escape-velocity' , {
title : 'Landing Page'
} ) ;
} ; จากนั้นสร้างเส้นทางใน app.js ฉันวางไว้ทันทีหลังจากตัวควบคุมดัชนี:
app . get ( '/escape-velocity' , homeController . escapeVelocity ) ; รีสตาร์ทเซิร์ฟเวอร์ (หากคุณไม่ได้ใช้ Nodemon ); จากนั้นคุณควรเห็นเทมเพลตใหม่ที่ http://localhost:8080/escape-velocity
ฉันจะหยุดตรงนี้ แต่ถ้าคุณต้องการใช้เทมเพลตนี้เป็นมากกว่าเพียงแค่หน้าเดียวลองดูว่าเทมเพลตปั๊กเหล่านี้ทำงานอย่างไร: layout.pug - เทมเพลตฐาน, index.pug - โฮมเพจ, partials/header.pug - bootstrap navbar, partials/footer.pug footer.pug คุณจะต้องแบ่งมันออกเป็นชิ้นเล็ก ๆ ด้วยตนเอง คิดว่าส่วนใดของเทมเพลตที่คุณต้องการเก็บไว้ในทุกหน้า - นั่นคือ layout.pug ใหม่ของคุณ จากนั้นแต่ละหน้าที่เปลี่ยนแปลงไม่ว่าจะเป็น index.pug , about.pug , contact.pug จะถูกฝังใน layout.pug ใหม่ของคุณผ่าน block content ใช้เทมเพลตที่มีอยู่เป็นข้อมูลอ้างอิง
นี่เป็นกระบวนการที่ค่อนข้างยาวและเทมเพลตที่คุณได้รับจากที่อื่นอาจมีระบบกริดอื่น นั่นเป็นเหตุผลที่ฉันเลือก Bootstrap สำหรับผู้เริ่มต้น Hackathon หลายคนคุ้นเคยกับ bootstrap แล้วรวมถึงการเริ่มต้นใช้งานง่ายถ้าคุณไม่เคยใช้ bootstrap นอกจากนี้คุณยังสามารถซื้อชุดรูปแบบ bootstrap ที่ออกแบบมาอย่างสวยงามมากมายที่ ThemeForest และใช้พวกเขาแทนการแทนที่ Hackathon Starter อย่างไรก็ตามหากคุณต้องการไปกับการออกแบบ HTML/CSS ที่กำหนดเองอย่างสมบูรณ์สิ่งนี้จะช่วยให้คุณเริ่มต้นได้!
ข้อความแฟลชอนุญาตให้คุณแสดงข้อความที่ส่วนท้ายของคำขอและเข้าถึงได้ในคำขอถัดไปและเฉพาะคำขอถัดไป ตัวอย่างเช่นในความพยายามในการเข้าสู่ระบบที่ล้มเหลวคุณจะแสดงการแจ้งเตือนด้วยข้อความแสดงข้อผิดพลาด แต่ทันทีที่คุณรีเฟรชหน้านั้นหรือเยี่ยมชมหน้าอื่นและกลับมาที่หน้าเข้าสู่ระบบข้อความแสดงข้อผิดพลาดนั้นจะหายไป มันจะแสดงเพียงครั้งเดียว โครงการนี้ใช้โมดูล Express-Flash สำหรับข้อความแฟลช และโมดูลนั้นถูกสร้างขึ้นที่ด้านบนของ Connect-Flash ซึ่งเป็นสิ่งที่ฉันใช้ในโครงการนี้ในตอนแรก ด้วย Express-Flash คุณไม่จำเป็นต้องส่งข้อความแฟลชไปยังทุกมุมมองภายใน res.render() อย่างชัดเจน ข้อความแฟลชทั้งหมดมีอยู่ในมุมมองของคุณผ่านทางวัตถุ messages โดยค่าเริ่มต้นด้วย การส่งแฟลชด่วน
ข้อความแฟลชมีกระบวนการสองขั้นตอน คุณใช้ req.flash('errors', { msg: 'Error messages goes here' } เพื่อสร้างข้อความแฟลชในคอนโทรลเลอร์ของคุณแล้วแสดงในมุมมองของคุณ:
if messages . errors
.alert.alert-danger.fade.in
for error in messages . errors
div = error . msg ในขั้นตอนแรก 'errors' คือชื่อของข้อความแฟลชซึ่งควรตรงกับชื่อของคุณสมบัติบนวัตถุ messages ในมุมมองของคุณ คุณวางข้อความการแจ้งเตือนไว้ใน if message.errors เพราะคุณไม่ต้องการแสดงข้อความแฟลชมีอยู่ เหตุผลที่คุณผ่านข้อผิดพลาดเช่น { msg: 'Error message goes here' } แทนที่จะเป็นเพียงแค่สตริง - 'Error message goes here' คือเพื่อความสอดคล้อง เพื่อชี้แจงว่าโมดูล Express-Validator ซึ่งใช้สำหรับการตรวจสอบความถูกต้องและการป้อนข้อมูลของผู้ใช้ในการฆ่าเชื้อจะส่งคืนข้อผิดพลาดทั้งหมดเป็นอาร์เรย์ของวัตถุที่แต่ละวัตถุมีคุณสมบัติ msg พร้อมข้อความว่าทำไมเกิดข้อผิดพลาด นี่คือตัวอย่างทั่วไปของสิ่งที่ Express-Validator ส่งคืนเมื่อมีข้อผิดพลาดอยู่:
[
{ param : "name" , msg : "Name is required" , value : "<received input>" } ,
{ param : "email" , msg : "A valid email is required" , value : "<received input>" }
] เพื่อให้สอดคล้องกับสไตล์นั้นคุณควรส่งข้อความแฟลชทั้งหมดเป็น { msg: 'My flash message' } แทนสตริง มิฉะนั้นคุณจะเห็นกล่องการแจ้งเตือนโดยไม่มีข้อความแสดงข้อผิดพลาด นั่นเป็นเพราะใน partials/flash.pug เทมเพลตมันจะพยายามเอาต์พุต error.msg (เช่น "My flash message".msg ) กล่าวอีกนัยหนึ่งมันจะพยายามเรียก msg ผงชูรสบนวัตถุ สตริง ซึ่งจะส่งคืน ไม่ได้กำหนด ทุกสิ่งที่ฉันเพิ่งพูดถึงเกี่ยวกับข้อผิดพลาดยังใช้กับข้อความแฟลช "ข้อมูล" และ "ความสำเร็จ" และคุณสามารถสร้างข้อความใหม่ด้วยตัวคุณเองเช่น:
ตัวควบคุมการใช้ข้อมูล (ตัวอย่าง)
req.flash('warning', { msg: 'You have exceeded 90% of your data usage' });
หน้าบัญชีผู้ใช้ (ตัวอย่าง)
if messages . warning
.alert.alert-warning.fade.in
for warning in messages . warning
div = warning . msg partials/flash.pug เป็นเทมเพลตบางส่วนที่มีวิธีการจัดรูปแบบข้อความแฟลช ก่อนหน้านี้ข้อความแฟลชกระจัดกระจายในแต่ละมุมมองที่ใช้ข้อความแฟลช (ติดต่อ, เข้าสู่ระบบ, ลงชื่อสมัครใช้, โปรไฟล์) แต่ตอนนี้ขอบคุณใช้วิธี การแห้ง
เทมเพลตบางส่วนของข้อความแฟลชจะ รวมอยู่ ใน layout.pug พร้อมกับส่วนท้ายและการนำทาง
body
include partials/header
.container
include partials/flash
block content
include partials/footerหากคุณมีคำถามเพิ่มเติมเกี่ยวกับข้อความแฟลชโปรดเปิดปัญหาและฉันจะอัปเดตคำแนะนำขนาดเล็กนี้ตามลำดับหรือส่งคำขอดึงหากคุณต้องการรวมสิ่งที่ฉันพลาด
A more correct way to say this would be "How do I create a new route?" The main file app.js contains all the routes. Each route has a callback function associated with it. Sometimes you will see three or more arguments for a route. In a case like that, the first argument is still a URL string, while middle arguments are what's called middleware. Think of middleware as a door. If this door prevents you from continuing forward, you won't get to your callback function. One such example is a route that requires authentication.
app . get ( '/account' , passportConfig . isAuthenticated , userController . getAccount ) ; It always goes from left to right. A user visits /account page. Then isAuthenticated middleware checks if you are authenticated:
exports . isAuthenticated = ( req , res , next ) => {
if ( req . isAuthenticated ( ) ) {
return next ( ) ;
}
res . redirect ( '/login' ) ;
} ; If you are authenticated, you let this visitor pass through your "door" by calling return next(); - It then proceeds to the next middleware until it reaches the last argument, which is a callback function that typically renders a template on GET requests or redirects on POST requests. In this case, if you are authenticated, you will be redirected to the Account Management page; otherwise, you will be redirected to the Login page.
exports . getAccount = ( req , res ) => {
res . render ( 'account/profile' , {
title : 'Account Management'
} ) ;
} ; Express.js has app.get , app.post , app.put , app.delete , but for the most part, you will only use the first two HTTP verbs, unless you are building a RESTful API. If you just want to display a page, then use GET , if you are submitting a form, sending a file then use POST .
Here is a typical workflow for adding new routes to your application. Let's say we are building a page that lists all books from the database.
Step 1. Start by defining a route.
app . get ( '/books' , bookController . getBooks ) ;Note: As of Express 4.x you can define your routes like so:
app . route ( '/books' )
. get ( bookController . getBooks )
. post ( bookController . createBooks )
. put ( bookController . updateBooks )
. delete ( bookController . deleteBooks )And here is how a route would look if it required an authentication and an authorization middleware:
app . route ( '/api/twitter' )
. all ( passportConfig . isAuthenticated )
. all ( passportConfig . isAuthorized )
. get ( apiController . getTwitter )
. post ( apiController . postTwitter ) Use whichever style makes sense to you. Either one is acceptable. I think that chaining HTTP verbs on app.route is a very clean and elegant approach, but on the other hand, I can no longer see all my routes at a glance when you have one route per line.
Step 2. Create a new schema and a model Book.js inside the models directory.
const mongoose = require ( 'mongoose' ) ;
const bookSchema = new mongoose . Schema ( {
name : String
} ) ;
const Book = mongoose . model ( 'Book' , bookSchema ) ;
module . exports = Book ; Step 3. Create a new controller file called book.js inside the controllers directory.
/**
* GET /books
* List all books.
*/
const Book = require ( '../models/Book.js' ) ;
exports . getBooks = ( req , res ) => {
Book . find ( ( err , docs ) => {
res . render ( 'books' , { books : docs } ) ;
} ) ;
} ; Step 4. Import that controller in app.js .
const bookController = require ( './controllers/book' ) ; Step 5. Create books.pug template.
extends layout
block content
.page-header
h3 All Books
ul
for book in books
li = book . nameแค่ไหน! I will say that you could have combined Step 1, 2, 3 as following:
app . get ( '/books' , ( req , res ) => {
Book . find ( ( err , docs ) => {
res . render ( 'books' , { books : docs } ) ;
} ) ;
} ) ; Sure, it's simpler, but as soon as you pass 1000 lines of code in app.js it becomes a little challenging to navigate the file. I mean, the whole point of this boilerplate project was to separate concerns, so you could work with your teammates without running into MERGE CONFLICTS . Imagine you have four developers working on a single app.js , I promise you it won't be fun resolving merge conflicts all the time. If you are the only developer, then it's okay. But as I said, once it gets up to a certain LoC size, it becomes difficult to maintain everything in a single file.
That's all there is to it. Express.js is super simple to use. Most of the time you will be dealing with other APIs to do the real work: Mongoose for querying database, socket.io for sending and receiving messages over WebSockets, sending emails via Nodemailer, form validation using validator.js library, parsing websites using Cheerio, etc.
Dan Stroot submitted an excellent pull request that adds a real-time dashboard with socket.io. And as much as I'd like to add it to the project, I think it violates one of the main principles of the Hackathon Starter:
When I started this project, my primary focus was on simplicity and ease of use. I also tried to make it as generic and reusable as possible to cover most use cases of hackathon web apps, without being too specific .
When I need to use socket.io, I really need it, but most of the time - I don't. But more importantly, WebSockets support is still experimental on most hosting providers. Due to past provider issues with WebSockets, I have not include socket.io as part of the Hackathon Starter. For now... If you need to use socket.io in your app, please continue reading.
First, you need to install socket.io:
npm install socket . io Replace const app = express(); ด้วยรหัสต่อไปนี้:
const app = express ( ) ;
const server = require ( 'http' ) . Server ( app ) ;
const io = require ( 'socket.io' ) ( server ) ; I like to have the following code organization in app.js (from top to bottom): module dependencies, import controllers, import configs, connect to database, express configuration, routes, start the server, socket.io stuff. That way I always know where to look for things.
Add the following code at the end of app.js :
io . on ( 'connection' , ( socket ) => {
socket . emit ( 'greet' , { hello : 'Hey there browser!' } ) ;
socket . on ( 'respond' , ( data ) => {
console . log ( data ) ;
} ) ;
socket . on ( 'disconnect' , ( ) => {
console . log ( 'Socket disconnected' ) ;
} ) ;
} ) ;One last thing left to change:
app . listen ( app . get ( 'port' ) , ( ) => {ถึง
server . listen ( app . get ( 'port' ) , ( ) => {At this point, we are done with the back-end.
You now have a choice - to include your JavaScript code in Pug templates or have all your client-side JavaScript in a separate file - in app.js . I admit, when I first started with Node.js and JavaScript in general, I placed all JavaScript code inside templates because I have access to template variables passed in from Express right then and there. It's the easiest thing you can do, but also the least efficient and harder to maintain. Since then I almost never include inline JavaScript inside templates anymore.
But it's also understandable if you want to take the easier road. Most of the time you don't even care about performance during hackathons, you just want to "get shit done" before the time runs out. Well, either way, use whichever approach makes more sense to you. At the end of the day, it's what you build that matters, not how you build it.
If you want to stick all your JavaScript inside templates, then in layout.pug - your main template file, add this to head block.
script ( src = '/socket.io/socket.io.js' )
script .
let socket = io . connect ( window . location . href );
socket . on ( ' greet ' , function ( data ) {
console . log (data);
socket . emit ( ' respond ' , { message : ' Hey there, server! ' });
}); Note: Notice the path of the socket.io.js , you don't actually have to have socket.io.js file anywhere in your project; it will be generated automatically at runtime.
If you want to have JavaScript code separate from templates, move that inline script code into app.js , inside the $(document).ready() function:
$ ( document ) . ready ( function ( ) {
// Place JavaScript code here...
let socket = io . connect ( window . location . href ) ;
socket . on ( 'greet' , function ( data ) {
console . log ( data ) ;
socket . emit ( 'respond' , { message : 'Hey there, server!' } ) ;
} ) ;
} ) ;And we are done!
Declares a read-only named constant.
const name = 'yourName' ;Declares a block scope local variable.
let index = 0 ; Using the `${}` syntax, strings can embed expressions.
const name = 'Oggy' ;
const age = 3 ;
console . log ( `My cat is named ${ name } and is ${ age } years old.` ) ; To import functions, objects, or primitives exported from an external module. These are the most common types of importing.
const name = require ( 'module-name' ) ; const { foo , bar } = require ( 'module-name' ) ;To export functions, objects, or primitives from a given file or module.
module . exports = { myFunction } ; module . exports . name = 'yourName' ; module . exports = myFunctionOrClass ; The spread operator allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.
myFunction ( ... iterableObject ) ; < ChildComponent { ... this . props } /> A Promise is used in asynchronous computations to represent an operation that hasn't completed yet but is expected in the future.
var p = new Promise ( function ( resolve , reject ) { } ) ; The catch() method returns a Promise and deals with rejected cases only.
p . catch ( function ( reason ) { /* handle rejection */ } ) ; The then() method returns a Promise. It takes two arguments: callback for the success & failure cases.
p . then ( function ( value ) { /* handle fulfillment */ } , function ( reason ) { /* handle rejection */ } ) ; The Promise.all(iterable) method returns a promise that resolves when all of the promises in the iterable argument have resolved or rejects with the reason of the first passed promise that rejects.
Promise . all ( [ p1 , p2 , p3 ] ) . then ( function ( values ) { console . log ( values ) } ) ; Arrow function expression. Shorter syntax & lexically binds the this value. Arrow functions are anonymous.
singleParam => { statements } ( ) => { statements } ( param1 , param2 ) => expression const arr = [ 1 , 2 , 3 , 4 , 5 ] ;
const squares = arr . map ( x => x * x ) ; The class declaration creates a new class using prototype-based inheritance.
class Person {
constructor ( name , age , gender ) {
this . name = name ;
this . age = age ;
this . gender = gender ;
}
incrementAge ( ) {
this . age ++ ;
}
}- Credits : DuckDuckGo and @DrkSephy.
- back to top
Math . floor ( Date . now ( ) / 1000 ) ; moment().unix();
var now = new Date ( ) ;
now . setMinutes ( now . getMinutes ( ) + 30 ) ; moment().add(30, 'minutes');
// DD-MM-YYYY
var now = new Date ( ) ;
var DD = now . getDate ( ) ;
var MM = now . getMonth ( ) + 1 ;
var YYYY = now . getFullYear ( ) ;
if ( DD < 10 ) {
DD = '0' + DD ;
}
if ( MM < 10 ) {
MM = '0' + MM ;
}
console . log ( MM + '-' + DD + '-' + YYYY ) ; // 03-30-2016 console.log(moment(new Date(), 'MM-DD-YYYY'));
// hh:mm (12 hour time with am/pm)
var now = new Date ( ) ;
var hours = now . getHours ( ) ;
var minutes = now . getMinutes ( ) ;
var amPm = hours >= 12 ? 'pm' : 'am' ;
hours = hours % 12 ;
hours = hours ? hours : 12 ;
minutes = minutes < 10 ? '0' + minutes : minutes ;
console . log ( hours + ':' + minutes + ' ' + amPm ) ; // 1:43 am console.log(moment(new Date(), 'hh:mm A'));
var today = new Date ( ) ;
var nextWeek = new Date ( today . getTime ( ) + 7 * 24 * 60 * 60 * 1000 ) ; moment().add(7, 'days');
var today = new Date ( ) ;
var yesterday = date . setDate ( date . getDate ( ) - 1 ) ; moment().add(-1, 'days');
- back to top
User . find ( ( err , users ) => {
console . log ( users ) ;
} ) ; let userEmail = '[email protected]' ;
User . findOne ( { email : userEmail } , ( err , user ) => {
console . log ( user ) ;
} ) ; User
. find ( )
. sort ( { _id : - 1 } )
. limit ( 5 )
. exec ( ( err , users ) => {
console . log ( users ) ;
} ) ; Let's suppose that each user has a votes field and you would like to count the total number of votes in your database across all users. One very inefficient way would be to loop through each document and manually accumulate the count. Or you could use MongoDB Aggregation Framework instead:
User . aggregate ( { $group : { _id : null , total : { $sum : '$votes' } } } , ( err , votesCount ) => {
console . log ( votesCount . total ) ;
} ) ;- back to top
You will need to install docker and docker-compose on your system. If you are using WSL, you will need to install Docker Desktop on Windows and docker-compose on WSL.
Docker installation
Common problems setting up docker
After installing docker, start the application with the following commands :
# To build the project while supressing most of the build messages
docker-compose build web
# To build the project without supressing the build messages or using cached data
docker-compose build --no-cache --progress=plain web
# To start the application (or to restart after making changes to the source code)
docker-compose up web
To view the app, find your docker IP address + port 8080 ( this will typically be http://localhost:8080/ ). To use a port other than 8080, you would need to modify the port in app.js, Dockerfile, and docker-compose.yml.
Once you are ready to deploy your app, you will need to create an account with a cloud platform to host it. These are not the only choices, but they are my top picks. Additionally, you can create an account with MongoDB Atlas and then pick one of the providers below. Again, there are plenty of other choices, and you are not limited to just the ones listed below.
Render provides free nodejs hosting for repos on Github and Gitlab.
0.0.0.0/0 . Click SAVE to save the 0.0.0.0/0 whitelist..env.example with this URI string. Make sure to replace the with the db User password that you created under the Security tab.We are deploying your changes . You will need to wait for the deployment to finish before using the DB in your application.sudo gem install rhc ?rhc login and enter your OpenShift credentialsrhc app create MyApp nodejs-0.10git remote add openshift YOUR_GIT_REMOTE Add these two lines to app.js , just place them anywhere before app.listen() :
var IP_ADDRESS = process . env . OPENSHIFT_NODEJS_IP || '127.0.0.1' ;
var PORT = process . env . OPENSHIFT_NODEJS_PORT || 8080 ; Then change app.listen() to:
app . listen ( PORT , IP_ADDRESS , ( ) => {
console . log ( `Express server listening on port ${ PORT } in ${ app . settings . env } mode` ) ;
} ) ; Add this to package.json , after name and version . This is necessary because, by default, OpenShift looks for server.js file. And by specifying supervisor app.js it will automatically restart the server when node.js process crashes.
"main" : "app.js" ,
"scripts" : {
"start" : "supervisor app.js"
} ,git push -f openshift master-f (force) flag because OpenShift creates a dummy server with the welcome page when you create a new Node.js app. Passing -f flag will override everything with your Hackathon Starter project repository. Do not run git pull as it will create unnecessary merge conflicts.git remote add azure [Azure Git URL]git push azure masterNOTE At this point it appears that Bluemix's free tier to host NodeJS apps is limited to 30 days. If you are looking for a free tier service to host your app, Render might be a better choice at this point
Create a Bluemix Account
Sign up for Bluemix, or use an existing account.
Download and install the Cloud Foundry CLI to push your applications to Bluemix.
Create a manifest.yml file in the root of your application.
applications:
- name: <your-app-name>
host: <your-app-host>
memory: 128M
services:
- myMongo-db-name
The host you use will determinate your application URL initially, eg <host>.mybluemix.net . The service name 'myMongo-db-name' is a declaration of your MongoDB service. If you are using other services like Watson for example, then you would declare them the same way.
$ cf login -a https://api.ng.bluemix.net
$ cf create-service mongodb 100 [your-service-name]
Note: this is a free and experiment verion of MongoDB instance. Use the MongoDB by Compose instance for production applications:
$ cf create-service compose-for-mongodb Standard [your-service-name]'
Push the application
$ cf push
$ cf env <your-app-name >
(To view the *environment variables* created for your application)
Done , now go to the staging domain ( <host>.mybluemix.net ) and see your app running.
Be sure to check out the full list of Watson services to forwarder enhance your application functionality with a little effort. Watson services are easy to get going; it is simply a RESTful API call. Here is an example of a Watson Toner Analyzer to understand the emotional context of a piece of text that you send to Watson.
Virtual Assistant - Deliver consistent and intelligent customer care across all channels and touchpoints with conversational AI.
Natural Language Understanding - Analyze text to extract meta-data from content such as concepts, entities, keywords and more.
Discovery - Accelerate business decisions and processes with an AI-powered intelligent document understanding and content analysis platform.
Orchestrate - Hand off tedious tasks to Watson and never work the same way again.
List of Watson Services.
Download and install Node.js
Select or create a Google Cloud Platform Console project
Enable billing for your project (there's a $300 free trial)
Install and initialize the Google Cloud SDK
Create an app.yaml file at the root of your hackathon-starter folder with the following contents:
runtime : nodejs
env : flex
manual_scaling :
instances : 1 Make sure you've set MONGODB_URI in .env.example
Run the following command to deploy the hackathon-starter app:
gcloud app deployMonitor your deployed app in the Cloud Console
View the logs for your app in the Cloud Console
If you are starting with this boilerplate to build an application for prod deployment, or if after your hackathon you would like to get your project hardened for production use, see prod-checklist.md.
You can find the changelog for the project in: CHANGELOG.md
If something is unclear, confusing, or needs to be refactored, please let me know. Pull requests are always welcome, but due to the opinionated nature of this project, I cannot accept every pull request. Please open an issue before submitting a pull request. This project uses Airbnb JavaScript Style Guide with a few minor exceptions. If you are submitting a pull request that involves Pug templates, please make sure you are using spaces , not tabs.
The MIT License (MIT)
Copyright (c) 2014-2023 Sahat Yalkabov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.