node.js基礎知識小結,node.js小結

來源:互聯網
上載者:User

node.js基礎知識小結,node.js小結

javascript之前一般用於web前段開發,然而由於node.js的出現,用javascript開發後端程式也不再是一件複雜的事情。node.js中js引擎來自於chrome v8瀏覽器,配合node.js額外開發的工具代碼,本身使用起來非常容易,也很高效。除此之外,node.js也有pip一樣的工具npm,使用npm可以輕鬆地安裝第三方軟體,這給我們的開發工作帶來了極大的方便。乘著周末,學習一下node.js,確實不錯。

1、安裝node.js

sudo apt-get install nodejs

2、安裝npm

sudo apt-get install npm

3、嘗試安裝express架構

npm install express --save

4、編寫最簡單的hello.js,用nodejs hello.js來執行

console.log('hello world')

5、複雜一點的http伺服器代碼

const http = require('http');const hostname = '127.0.0.1';const port = 3000;const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n');});server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`);});

6、使用express開發http伺服器

var express = require('express');var app = express(); app.get('/', function (req, res) {  res.send('Hello World');}) var server = app.listen(8081, function () {  var host = server.address().address var port = server.address().port  console.log("access url is http://%s:%s", host, port) })

7、剩下來的事情

node.js有很多的架構,也有很多的第三方庫,項目使用起來很方便,歡迎大家多多使用、多多練習。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.