<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pansit Wattanaprasobsuk on Medium]]></title>
        <description><![CDATA[Stories by Pansit Wattanaprasobsuk on Medium]]></description>
        <link>https://medium.com/@pansit?source=rss-177ecd676eff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fk9ucVBMRNUveM9GHUVjRA.jpeg</url>
            <title>Stories by Pansit Wattanaprasobsuk on Medium</title>
            <link>https://medium.com/@pansit?source=rss-177ecd676eff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 00:13:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pansit/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[รวมร่าง GraphQL และ REST API มาเขียนรอบเดียวได้ทั้ง 2 Protocals ด้วย Sofa]]></title>
            <link>https://medium.com/@pansit/%E0%B8%A3%E0%B8%A7%E0%B8%A1%E0%B8%A3%E0%B9%88%E0%B8%B2%E0%B8%87-graphql-%E0%B9%81%E0%B8%A5%E0%B8%B0-rest-api-%E0%B8%A1%E0%B8%B2%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%A3%E0%B8%AD%E0%B8%9A%E0%B9%80%E0%B8%94%E0%B8%B5%E0%B8%A2%E0%B8%A7%E0%B9%84%E0%B8%94%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%89%E0%B8%87-2-protocals-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-sofa-67f73b8a09f0?source=rss-177ecd676eff------2</link>
            <guid isPermaLink="false">https://medium.com/p/67f73b8a09f0</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[prisma]]></category>
            <category><![CDATA[sofa]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Pansit Wattanaprasobsuk]]></dc:creator>
            <pubDate>Sun, 27 Jan 2019 11:25:57 GMT</pubDate>
            <atom:updated>2019-01-27T14:59:36.146Z</atom:updated>
            <content:encoded><![CDATA[<p>GraphQL ❤️ RestAPI by SOFA with Prisma</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MuG0B9DDPlla5bVQlNaHug.png" /><figcaption>GraphQL ❤️Rest</figcaption></figure><blockquote>Reference จากบทความนี้</blockquote><blockquote><a href="https://medium.com/the-guild/sofa-the-best-way-to-rest-is-graphql-d9da6e8e7693">https://medium.com/the-guild/sofa-the-best-way-to-rest-is-graphql-d9da6e8e7693</a></blockquote><blockquote>Example ที่ยืมของ Prisma มาผสมกับโมเองนิดหน่อยเพื่อให้ใช้ได้กับ Sofa</blockquote><blockquote><a href="https://github.com/prisma/prisma-examples/tree/master/typescript/graphql-subscriptions">https://github.com/prisma/prisma-examples/tree/master/typescript/graphql-subscriptions</a></blockquote><h3>Requirements</h3><ul><li>Docker</li><li>Node.js</li><li>Prisma</li></ul><h3>ลองรันให้ก่อน</h3><ol><li>เริ่มจาก git clone <a href="https://github.com/pansitwattana/sofa-prisma-graphql-and-rest-api">https://github.com/pansitwattana/sofa-prisma-graphql-and-rest-api.git</a></li><li>docker-compose up -d รัน mysql และ prisma</li><li>เปิด <a href="http://localhost:14466/">http://localhost:14466</a> ให้ขึ้นหน้า graphql playground ของ Prisma ก่อน มันจะเป็นว่างๆ ยังทำไรไม่ได้</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ttvgWR763DXTvugiEZMarA.png" /><figcaption>ด้านขวา schema กับ docs จะไม่มีอะไรในตอนแรก</figcaption></figure><p>เข้าไปใน Folder ที่ clone มา</p><ol><li>npm install รอบนึง</li><li>npm install -g prisma</li><li>prisma deploy อัพเดทตาราง mysql ให้ตรงตาม schema</li><li>prisma seed</li><li>npm run generate คำสั่งนี้จะใช้ทุกครั้งที่มีการอัพเดท schema</li><li>ลองเปิด <a href="http://localhost:14466/">http://localhost:14466</a> อีกรอบคราวนี้จะมี schema ขึ้นมาละ</li><li>ลอง Query จาก Prisma ดูจะได้ค่ามาละตามที่เรา seed ไป</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_F7KWYZJz-_LYjXLwas_xg.png" /></figure><pre>{<br>  posts {<br>    id<br>    title<br>    content<br>    createdAt<br>  }<br>}</pre><p>อันข้างบนคือการ query ตรงๆจาก database ผ่านตัว Prisma เลยซึ่งเอาไว้ทดสอบเท่านั้น เพราะถ้าให้ Client เข้าถึงได้ ทุกคนจะสามารถเพิ่มหรือลบหรือแก้ไขได้ทุกอย่างเลย</p><h4>สร้าง server มาครอบอีกชั้นจาก Node.js</h4><p>เพื่อที่สามารถเพิ่ม Logic หรือนำมาทำ Authentication เพิ่มตรงนี้ได้</p><ol><li>npm start</li><li>เปิด <a href="http://localhost:8000/graphql">http://localhost:8000/graphql</a> ดูจะเห็นแค่ schema ที่จะให้ฝั่ง client เห็นเท่านั้น ตามในไฟล์ src/schema.graphql</li></ol><h3>ลอง Query เล่น (GraphQL)</h3><p>schema ตรงนี้จะอยู่ที่ไฟล์ src/schema.graphql</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Ns6dThHTELr32nzdg8_jeQ.png" /></figure><h4>Feed</h4><p>ลองเรียก feed api (เอา Post ที่ published เป็น true เท่านั้น)</p><p>จากไฟล์ src/resolvers/Query.ts เป็นโค๊ดดึง posts ที่ published เป็น true</p><pre>...<br>feed: (parent, args, context) =&gt; {<br>   return context.prisma.posts({ where: { published: true } })<br>}<br>...</pre><p>ข้อมูลที่ได้เป็น Array ของ Post ที่มี field แค่ id, title, content ตามที่ส่งไป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rKiT_NB3zjUPla8kstePhA.png" /></figure><pre>{<br> feed {<br>    id<br>    title<br>    content<br>  }<br>}</pre><h4>Post by id</h4><p>ลองส่ง id ไป get post นั้นๆ กันดู จะได้เป็น post เป็น object</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oCoS8G7Fm1uDEw9M2762EA.png" /></figure><pre>{<br>  post(id: &quot;cjrepa29y000g0950qaana3hg&quot;) {<br>    id<br>    title<br>    content<br>  }<br>}</pre><h4>Filter Posts</h4><p>ค้นหา post ที่มี title ว่า “conf” จะได้ผลลัพธ์มาอันเดียว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HZR0TCPCcydd6L-V8EoWIQ.png" /></figure><pre>{<br> filterPosts(searchString: &quot;conf&quot;) {<br>    id<br>    title<br>    content<br>  }<br>}</pre><h3>มาลอง Queryโดยใช้ Rest API บ้าง</h3><p>โค๊ดของ sofa ที่เอามาใช้ง่ายมาก อยู่ใน index.ts</p><p><strong>แค่ 3 บรรทัดก็ทำให้ GraphQL สามารถมี Rest API ได้แล้ว!!!</strong></p><pre>const typeDefs = importSchema(&#39;./src/schema.graphql&#39;)</pre><pre>const schema = makeExecutableSchema({ typeDefs, resolvers: resolvers as IResolvers })</pre><pre>server.use(&#39;/rest&#39;, sofa({ schema, context: { prisma } }));</pre><h4>Feed</h4><p>หน้าตา route ก็ประมาณนี้ <a href="http://localhost:8000/rest/feed">http://localhost:8000/rest/feed</a> มัน easy มากกกกก</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r6B3F7cJdMXbNVfgrRYlyQ.png" /><figcaption>ผลลัพธ์ที่ได้</figcaption></figure><h4>Post by id</h4><p>คราวนี้สามารถส่ง params id ไปได้ด้วย <a href="http://localhost:8000/rest/feed">http://localhost:8000/rest/p</a>ost/:id (id ก็ลองก๊อปมาจาก feed ดูซึ่งมันจะ random ขึ้นมาไม่เหมือนกัน)</p><p><a href="http://localhost:8000/rest/post/cjrepa29y000g0950qaana3hg">http://localhost:8000/rest/post/cjrepa29y000g0950qaana3hg</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RqLaf8aCVnrlP9RVoDFb5Q.png" /><figcaption>ผลลัพธ์ที่ได้</figcaption></figure><h4><strong>Filter Post</strong></h4><p>ถ้ากรณีนี้ params ไม่ได้เป็น id การส่งไปจะส่งโดย query string และสังเกตุ route นี้จะเป็น filter-post นะเพราะปกติการกำหนด route จะไม่ใช้ camelCase กัน</p><p><a href="http://localhost:8000/rest/filter-posts?searchString=graphql">http://localhost:8000/rest/filter-posts?searchString=conf</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*SmsLzjisvOaPQP45I0JtZg.png" /><figcaption>ผลลัพธ์ที่ได้</figcaption></figure><h3>สรุปหน่อย</h3><p>ข้อดีข้อเสียของทั้ง ​GraphQL กับ Rest นั้นคงไปหาอ่านตามบล็อคอื่นกันดู มันก็แล้วแต่ว่าเราต้องใช้งานแบบไหน Sofa ก็ตอบโจทย์ตรงสามารถทำให้เป็น Hybrid ได้เลยไม่ต้อง Implement หลายรอบ</p><p>ถ้าคร่าวๆการใช้งาน GraphQL นั้นดูจะเหมาะกับ Frontend มากกว่าทำให้ทำงานง่ายขึ้น แต่ถ้าเป็น Microservice แบบ server ต้องการคุยกับ server กันเองนี้ Rest จะใช้ง่ายกว่า</p><h4>ใครที่ยังงงๆ เข้ามาได้ยังไง</h4><ul><li>ถ้าไม่รู้จัก Node.js มันเป็นสิ่งที่ Backend Developer ยุคใหม่ควรรู้จัก <a href="https://nodejs.org/en/">https://nodejs.org</a></li><li>ถ้าเพิ่งเคยได้ยิน GraphQL เป็น Trend ใหม่มาแรง อ่านเพิ่มเติมได้ใน <a href="https://graphql.org/">https://graphql.org/</a></li><li>Prisma ก็เป็นตัวนึงทำให้ขึ้น GraphQL ได้เร็ว <a href="https://prisma.io/">https://prisma.io</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67f73b8a09f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ตอนที่ 4: Get to know React]]></title>
            <link>https://medium.com/blogs-194/%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-4-get-to-know-react-b4007cfe586b?source=rss-177ecd676eff------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4007cfe586b</guid>
            <dc:creator><![CDATA[Pansit Wattanaprasobsuk]]></dc:creator>
            <pubDate>Tue, 16 May 2017 05:20:32 GMT</pubDate>
            <atom:updated>2017-05-16T05:20:32.520Z</atom:updated>
            <content:encoded><![CDATA[<p>หลังจากสัปดาห์ที่แล้วที่เราติดตั้งตัว Node js ที่เป็น Back End กันเสร็จเรียบร้อย ต่อมาสัปดาห์นี้เราจะลองมาเขียนตัว Hello World กันบนฝั่ง Front End ซึ่งวันนี้เราจะลองใช้ตัว React ซึ่งเป็น Javascript Library ของตัว Facebook</p><p>ขั้นตอนแรกเรามาลองติดตั้งตัว Javascript Library กัน</p><ol><li>สร้าง Folder ที่เก็บ Project React จากนั้น พิมคำสั่งตามนี้</li></ol><pre><br> npm install -g create-react-app<br> create-react-app hello-world<br> cd hello-world<br> npm start</pre><p>**Npm จะทำการติดตั้ง Library ที่จำเป็นพร้อมทั้งสร้าง Project hello-world ขึ้นมา</p><p>2. เมื่อติดตั้งสำเร็จแล้ว Cmd จะบอกว่า Project ของเราจะรันที่ <a href="http://localhost:3000/">http://localhost:3000/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*nsC-SY81eFu8j0Ra." /></figure><p>** ใครได้หน้าตามนี้ถือว่าติดตั้งเสร็จเรียบร้อยแล้วครับ<br>3. ใน Folder hello-world เราจะได้โครงสร้างที่ประกอบด้วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*0ivpyJCa382BTJZC." /></figure><p>node_modules : เป็นที่เก็บตัว Lib ต่างๆของ Project<br>public : เป็นที่เก็บไฟล์ html เป็นตัวแสดงผลหน้าเว็บ<br>src : เป็นที่เก็บไฟล์ .js เป็นที่เราจะเขียน Javascript กัน<br>package.Json : เป็นไฟล์ ที่เก็บข้อมูล Metadata ของ Project พร้อมรายชื่อ node_modules ด้วย</p><p>4. อันนี้สำคัญมาก เราจะเขียน Hello World กัน</p><p>เข้า Folder src จะพบกับไฟล์ต่างๆ ให้เราแก้ไข ไฟล์ index.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*HL7egCRiTaKbUWRs." /></figure><p>เข้าไปแก้ไข Code ในส่วน ReactDOM.render ตามด้านล่าง</p><p>5. เข้า Cmd และลอง Run Project ของเรากันโดยการกด Shift + คลิกขวา เลือก Open command window here</p><p>จากนั้นพิม npm start</p><p>รอโปรแกรม Run ซักแปป จะได้หน้า hello world แล้ววววว</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4007cfe586b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/blogs-194/%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-4-get-to-know-react-b4007cfe586b">ตอนที่ 4: Get to know React</a> was originally published in <a href="https://medium.com/blogs-194">Technologies For Everyone</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Node Package Manager + Express]]></title>
            <link>https://medium.com/blogs-194/node-package-manager-express-74aec887d76?source=rss-177ecd676eff------2</link>
            <guid isPermaLink="false">https://medium.com/p/74aec887d76</guid>
            <dc:creator><![CDATA[Pansit Wattanaprasobsuk]]></dc:creator>
            <pubDate>Sun, 16 Apr 2017 07:08:20 GMT</pubDate>
            <atom:updated>2017-04-16T07:08:20.722Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8SRCBx55q35QmFk1aEVH6w.png" /></figure><p>npm หรือ node package manager มันทำให้ชีวิตเราง่ายขึ้นในการที่ใช้ code ของคนอื่นที่ทำให้เราแล้ว หรือเราอยากจะแชร์ code เราให้ใครก็ได้ เมื่อ code มีอัพเดทเราก็สามารถเช็คได้แค่คำสั่งเดียว</p><p>เมื่อเราติดตั้ง node แล้ว npm จะถูกติดตั้งตามมาด้วย</p><p>ลองเช็ค version ใน cmd พิม npm --version</p><p>สร้างไฟล์ชื่อว่า app.js ขึ้นมา</p><p>หลังจากนั้นเปิด terminal จากการกด ctrl + ` หลังจากนั้นพิมพ์คำสั่ง npm init</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/176/1*zo8KuyFT2oTuQZwcUpzEVg.png" /></figure><p>มันก็จะให้เราพิมชื่อโปรเจคก็พิมๆไป</p><p>กด Enter รัวๆ จนเสร็จ</p><p>จะได้ไฟล์ชื่อ package.json มาไฟล์นี้จะเป็นตัวบอกว่าโปรเจคเราต้องมี module ไรเพื่อ install บ้าง</p><pre>{<br>  &quot;name&quot;: &quot;testnpm&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;main&quot;: &quot;app.js&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.15.2&quot;<br>  },<br>  &quot;devDependencies&quot;: {},<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;<br>}</pre><p>พอเห็นเจ้านี้อย่างเพิ่งตกใจ มันเป็นตัวบอกว่าโปรเจคเราต้องมี package อะไรติดตั้งบ้างซึ่งจะอยู่ใน <strong>dependencies</strong></p><p>เดียวเราจะมาลองติดตั้ง express ดู</p><blockquote>Express เป็น Framework ที่สร้างบน Node.js อีกทีเพื่อให้เราสร้างอะไรง่ายขึ้น</blockquote><p>เริ่มด้วยการ npm install express --save เสร็จลองดูที่ package.json จะมี express โผล่ขึ้นมาตรง dependencies และมี node_modules เป็นที่เก็บสิ่งที่เราลงไว้</p><pre>&quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.15.2&quot;<br>  },</pre><p>ซึ่ง dependencies นี้มีประโยชน์ตรงไหน</p><ol><li>มันทำให้เราสามารถติดตามเวอร์ชั้นใหม่ได้ตลอด เวลาอัพเดทเราแค่พิม npm install</li><li>เวลาเราแชร์โปรเจคกับเพื่อนเรา เราไม่จำเป็นที่จะต้องอัพ node_modules ขึ้นไปให้เพื่อน เพราะทุกอย่างอยู่ใน dependencies เราก็ส่งแค่โปรเจคเรากับ package.json อัพขึ้น git แล้วพอเพื่อน pull ลงมาก็แค่ npm install แค่นี้ก็จะได้อะไรที่เวอร์ชั่นล่าสุดทันทีเลย</li></ol><p>คราวนี้มาลองสร้างไฟล์ app.js</p><pre>var app = require(&#39;express&#39;)();</pre><pre>var port = process.env.PORT || 7777;</pre><pre>app.get(&#39;/&#39;, function (req, res) {<br>    res.send(&#39;&lt;h1&gt;Hello Node.js&lt;/h1&gt;&#39;);<br>});</pre><pre>app.get(&#39;/index&#39;, function (req, res) {<br>    res.send(&#39;&lt;h1&gt;This is index page&lt;/h1&gt;&#39;);<br>});</pre><pre>app.listen(port, function() {<br>    console.log(&#39;Starting node.js on port &#39; + port);<br>});</pre><p>บรรทัดแรก var app = require(‘express’)();เราจะดึง express ออกมาไว้ที่ app</p><p>บรรทัดที่สองเรากำหนด port เป็น 7777</p><p>บรรทัดที่สาม app.get(‘/’) คือการบอก path ในการ route ว่าถ้าเราพิม localhost:7777/ ในคำสั่งข้างในจะถูก execute</p><p>แต่ถ้าเรา app.get(‘/index’) คำสั่งจะถูก execute เมื่อเราพิมเช่น localhost:7777/index เป็นต้น</p><p>ส่วน app.listen บอกให้เริ่มเปิด port</p><p>เวลารันปกติเราจะรันเป็น node app.js เรามาลองใช้ npm กัน โดยแก้ใน package.json ในส่วนของ script เป็นแบบนี้</p><pre>&quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;node app.js&quot;<br>  },</pre><p>scripts คือเวลาเราพิม npm ตามด้วย … มันจะรันคำสั่งนั้น</p><p>เช่นในตัวอย่างถ้าเราพิม npm start -&gt; มันจะรัน node app.js</p><p>ซึ่งข้อดีตรงนี้คือบางทีเราอาจจะมีการรันหลายแบบ มีรับ args เราก็สร้างวิธีการรันแบบใหม่ให้มันโดยเราก็แก้ใน package.json ที่เดียว</p><p>ลองรันดู npm start</p><p>แล้วก็ลองเข้า Browser พิม localhost:7777/ กับ localhost:7777/index ดู</p><p>ทุกอย่างก็จะเหมือนกับในบทความที่แล้ว และจริงๆ npm มีให้ใช้มากมายลองเข้าไปดูได้ใน <a href="https://www.npmjs.com/">https://www.npmjs.com/</a> ซึ่งทั้งหมดนั้นฟรีหมดเลย npm ก็เป็นหนึ่งในจุดเด่นของ node.js เลย</p><p>ผมจะแนะนำให้ลง nodemon ไว้ซึ่ง nodemon มีไว้รันแทน node มันจะคอย monitor ถ้าเราเซฟไฟล์ปุป มันจะ restart แอพให้เราทันทีโดยที่ไม่ต้อง พิม npm start หรือ node app.js ใหม่อีก</p><p>วิธีลงก็พิม npm install nodemon -g</p><p>ขออธิบาย -g คือการติดตั้งเป็นแบบ global สามารถใช้ได้ทุกที่ในคอมเราและมันจะไม่อยู่ใน project เรา</p><p>ก็ลองใช้ nodemon app.js แทนในการรันดู</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74aec887d76" width="1" height="1" alt=""><hr><p><a href="https://medium.com/blogs-194/node-package-manager-express-74aec887d76">Node Package Manager + Express</a> was originally published in <a href="https://medium.com/blogs-194">Technologies For Everyone</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create Node.js Hello World Application]]></title>
            <link>https://medium.com/blogs-194/create-node-js-hello-world-application-5c59495490eb?source=rss-177ecd676eff------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c59495490eb</guid>
            <dc:creator><![CDATA[Pansit Wattanaprasobsuk]]></dc:creator>
            <pubDate>Fri, 31 Mar 2017 12:25:43 GMT</pubDate>
            <atom:updated>2017-03-31T12:25:43.102Z</atom:updated>
            <content:encoded><![CDATA[<p>ในตอนนี้เราจะมาลองสร้าง Server ใน Node.js จะมี module http ที่จัดการเรื่องนี้ไว้แล้ว เรามีหน้าที่แค่ต้องรู้วิธีใช้มัน มาเริ่มกันเลย</p><p>สร้างไฟล์ server.js ขึ้นมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*K2XUWGiVnBNP2ts_LGImTQ.png" /></figure><h4>เรียกใช้ Module http</h4><p>module มันเหมือนเรา include ใน ภาษา c ครับ ซึ่งทำให้เราสามารถใช้ function ข้างใน module นั้นได้</p><p>var http = require(&#39;http&#39;);</p><h4>function createServer</h4><p>เราสร้างการตอบกลับของ server (response) ก็คือเมื่อมีใครขอ request เข้ามาก็จะตอบกลับไปว่า Hello World</p><pre>var server = http.createServer(function(request, response){<br>	response.writeHead(200, {&#39;Content-Type&#39;: &#39;text/plain&#39;});<br>	response.end(&#39;Hello World&#39;);<br>});</pre><p>เราต้องบอก server ว่าจะสร้างบน ip อะไร port อะไร ในที่นี้เราใช้ localhost ซึ่งมี ip คือ 127.0.0.1 ส่วน port เราจะใช่เป็นอะไรก็ได้แล้วแต่เลย</p><pre>server.listen(3000, &#39;127.0.0.1&#39;);<br>console.log(&#39;Alien, we are listening to port 3000&#39;);</pre><h4>Run!</h4><p>คราวนี้เราก็จะมาสร้าง server กันละนะครับ</p><p>ใน Visual Code กด Ctrl + P แล้วพิมพ์ &gt; ตามด้วย View Toggle Integrated Terminal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h2UW3yq7od7VbkO6-0UFAg.png" /></figure><p>เพื่อเรียก Terminal ขึ้นมาใน Visual Code และใส่คำสั่ง</p><p>node server.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yPVYJaEWoVjyoUGqFImzrA.png" /></figure><p>แล้วกด Enter แค่นี้ก็สร้าง Server</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fW9UejC_doM1O5SDQLc7zw.png" /></figure><p>เห็นว่าใน Terminal เรา Server ก็บอกว่า Alien, we are listening to port 3000</p><p>ซึ่งเมื่อมีใครเข้ามา port 3000 server ก็จะตอบกลับด้วย Hello World</p><p>เข้า Chrome หรือ Browser ตัวอื่นๆ พิม address</p><p><a href="http://localhost3000/">http://localhost3000/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*naON2qO1exO7W9D4geDxHw.png" /></figure><p>เห็น Server ได้ทำการ Response กลับว่าตามที่เราเขียนไว้</p><h4>response html</h4><p>คราวนี้เราจะมาลองสิ่งที่เราต้องใช้จริงๆกันนั้นคือการตอบกลับด้วยไฟล์ html</p><p>จะมาลองส่งไฟล์ html เมื่อมีคนมาขอ request กับ server ของเรากันดูครับ</p><p>ขั้นแรกสร้างไฟล์ index.html ใหม่เป็นแบบนี้</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>    &lt;head&gt;<br>        &lt;style&gt;<br>            body{<br>                background: skyblue;<br>                font-family: verdana;<br>                color: #fff;<br>                padding: : 30px;<br>            }<br>            h1{<br>                font-size: 48px;<br>                text-transform: uppercase;<br>                letter-spacing:2px;<br>                text-align: center;<br>            }<br>            p{<br>                font-size: 16px;<br>                text-align: center;<br>            }<br>        &lt;/style&gt;    <br>    &lt;/head&gt;<br>&lt;body&gt;<br>        &lt;h1&gt;Welcome to Node.js&lt;/h1&gt;<br>        &lt;p&gt;Create Hello World tutorial&lt;/p&gt;<br>    &lt;/body&gt;<br>&lt;/html&gt;</pre><p>ขั้นที่สองเปลี่ยน server.js เป็น</p><pre>var http = require(&#39;http&#39;);<br>var fs = require(&#39;fs&#39;);<br>fs.readFile(&#39;./index.html&#39;, function(err, html){<br>    http.createServer(function(request, response){<br>        response.writeHeader(200, {&quot;Content-Type&quot;: &quot;text/html&quot;});  <br>        response.write(html);  <br>        response.end();  <br>    }).listen(3000, &#39;127.0.0.1&#39;);<br>})</pre><p>ส่วนแรก fs module เป็นตัวไว้อ่านไฟล์ เมื่ออ่านไฟล์ index.html เสร็จ http ก็จะสร้าง server มี Content-Type เป็น text/html</p><p>ลอง node server.js ดู</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YTWm1LbWqmmb08GNdbErNw.png" /></figure><p><strong>response json</strong></p><p>ถ้าเป็น json เราจะใส่ Content-Type เป็น application/json</p><pre>var http = require(&#39;http&#39;)<br>var server = http.createServer(function(req, res){<br>    //header<br>    console.log(&#39;request was made: &#39; + req.url);<br>    res.writeHead(200, {&#39;Content-Type&#39;: &#39;application/json&#39;});<br>    var myObj = {<br>        name: &#39;Ryu&#39;,<br>        job: &#39;Ninja&#39;,<br>        age: 29<br>    };<br>res.end(JSON.stringify(myObj));<br>});<br>server.listen(3000, &#39;127.0.0.1&#39;);<br>console.log(&#39;now listening to port 3000&#39;);</pre><p>เราสร้าง myObj เป็น Json ซึ่งตอนส่งเราต้องแปลงให้เป็น Json ก่อนโดยใช้ JSON.strigify</p><p>และเราก็จะสามารถส่งเป็น Json ได้ซึ่งตรงนี้สามารถนำไปประยุกต์กับการ request ไปขอ database ละให้ตอบกลับเป็น json ได้</p><h3><strong>ปิดท้าย</strong></h3><p>เราก็เห็นว่ามันจะเริ่มมีคำสั่งเยอะขึ้นเรื่อยๆต้องจำว่า Content-Type เป็นอะไร ในบทความหน้าเราจะมาลองใช้ Express กันซึ่งเป็น Package ที่จัดการเรื่องพวกนี้ให้หมดแล้วทั้งเรื่องการ Route การจัดการสร้าง server จัดการ Content-Type ให้เราหมดแล้ว</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c59495490eb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/blogs-194/create-node-js-hello-world-application-5c59495490eb">Create Node.js Hello World Application</a> was originally published in <a href="https://medium.com/blogs-194">Technologies For Everyone</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Node.js Introduction + Installation]]></title>
            <link>https://medium.com/blogs-194/node-js-introduction-installation-24aecdc67bf3?source=rss-177ecd676eff------2</link>
            <guid isPermaLink="false">https://medium.com/p/24aecdc67bf3</guid>
            <dc:creator><![CDATA[Pansit Wattanaprasobsuk]]></dc:creator>
            <pubDate>Fri, 24 Feb 2017 03:53:51 GMT</pubDate>
            <atom:updated>2017-03-08T06:31:04.317Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>What is Node.js ?</strong></h3><ul><li>Node.js เป็นสิ่งที่ทำให้เราสามารถใช้ JavaScript ในการรัน server ได้โดยที่เราไม่ต้องไปเรียนรู้ PHP หรือ Ruby เราสามารถทำทุกอย่างใน JavaScript แค่ภาษาเดียวพอแถมข้อดีอีกเพียบ</li><li>Node.js สามารถ read, delete หรือ update ไฟล์บน database เราได้ และยังสามารถติดต่อกับ database ได้ง่ายมาก</li><li>Node.js ยังมี library (open source packages) พร้อมให้ใช้เยอะมากซึ่งทำให้การพัฒนาเว็บของเราเร็วและง่ายมากขึ้น</li></ul><h3>Who is this for ?</h3><ul><li>คนที่อยากรู้เรื่อง Backend</li><li>คนที่เคยเขียน PHP backend มาแล้ว ถ้ามาลอง Node.js คุณจะพบว่า Node.js มันสุดยอดแค่ไหน มันมีอะไรหลายๆอย่างเตรียมพร้อมให้ใช้ทำงานเรียบร้อยแค่เราเรียกใช้มันแค่นั้น</li><li>อยากรู้เกี่ยวกับ Node.js</li></ul><h3><strong>What do you do with ?</strong></h3><ul><li>API (Web Sockets) ที่จัดการระบบข้างหลังให้เราหมดเช่นเรื่องการแปลง JSON ทำให้เราโฟกัสไปในเรื่องการพัฒนาได้มากขึ้น</li><li>Real-time Application ex. chat app</li></ul><h3><strong>Requirements</strong></h3><h4><strong>Node.js</strong></h4><p><strong>Download Node.js and Setup</strong></p><blockquote><a href="http://nodejs.org/download/">http://nodejs.org/download/</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6EnoJHIkOaa1smHc." /></figure><h4>ตรวจสอบว่าลงเสร็จแล้ว</h4><ol><li>เปิด Command Propmt ขึ้นมา (Ctrl + r พิม cmd) และพิม node -v</li></ol><blockquote>node -v</blockquote><p>ถ้าได้ประมาณนี้แปลว่าโอเคลงเสร็จแล้ว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*gJkksQbsCRc2VQNeqetNhA.png" /></figure><p>2. ตรวจสอบ version ของ NPM</p><blockquote>npm -v</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*8YOzFZzVC30aCH6vOx6Hvg.png" /></figure><p>3. ถ้าได้เวอร์ชั่นต่ำกว่านี้ หรืออยากจะลองเช็คว่ามีอัพเดทมั้ยก็ลองพิม</p><blockquote>npm install npm -g</blockquote><p>4. ลอง Hello World หน่อย ลองพิมคำสั่งตามนี้</p><blockquote>พิม node ก่อนแล้ว Enter</blockquote><blockquote>ตามด้วย console.log(“Hello World”);</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*-uwQ3uK9mBh3iEwu-1pfXA.png" /><figcaption>ถ้าขึ้นตามนี้แสดงว่าลงเสร็จแล้ว</figcaption></figure><h4><strong>Text Editor</strong></h4><p>ใช้เขียนโค๊ดเลือกเอาสักอันนึงที่ชอบ</p><ol><li>Visual Code</li><li>Sublime</li><li>Notepad++</li><li>Atom</li></ol><h3>สิ่งที่จะได้เรียนรู้หลังจากจบบล๊อค</h3><ul><li>รู้วิธีสร้าง api จาก node.js</li><li>สร้างเว็บแอพ To-do list โดยใช้ MangoDB</li></ul><h3>ตอนต่อไป</h3><p>เราจะมาลองสร้าง server บน localhost กันครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24aecdc67bf3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/blogs-194/node-js-introduction-installation-24aecdc67bf3">Node.js Introduction + Installation</a> was originally published in <a href="https://medium.com/blogs-194">Technologies For Everyone</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>