Chạy NodeJS trên Visual code
Khi học lập trình Node JS thì các bạn có thể sử dụng IDE hoặc Editor. Nếu sử dụng IDE thì bạn có thể sử dụng IDE tốt nhất cho Node chính là Webstorm của JetBrains, tuy nhiên công cụ này là trả phí và giá khá là cao nên mình không khuyến khích sử dụng nếu bạn là sinh viên hoặc là người mới nghiên cứu về Node JS. Bạn có thể tài bản dùng thử 30 ngày của Webstorm nếu muốn thử trả nghiệm. Show Cá nhân mình thì rất thích và đang sử dụng Atom của Github với một cộng đồng lớn mạnh hỗ trợ và hoàn toàn miễn phí. Ngoài ra còn có một công cụ khác miễn phí nữa chính là Visual Studio Code của Microsoft, đây cũng là công cụ rất được ưa thích. Và Editor cuối mình giới thiệu chính là Sublime Text , không cần nói nhiều đây là công cụ được ưa thích từ rất lâu, tuy nhiên theo đánh giá của cá nhân mình thì thứ nhất nó không hoàn toàn miễn phí, thứ hai các plugin hỗ trợ không đa dạng bằng Atom hoặc Visual Studio Code. Vậy tóm lại các bạn sẽ cài 1 trong 3 Editor dưới đây để thực hành nhé. 1. Atom : https://atom.io/ 2. Visual Studio Code : https://code.visualstudio.com/ 3. Sublime Text : https://www.sublimetext.com/
III. Chạy thử ứng dụng đầu tiên.Để làm được việc này các bạn chắc chắn phải cài được Node JS và NPM trên máy tính, có thể xem lại bài hướng dẫn cài đặt nếu các bạn chưa làm được bước này.
Bước 1. Khởi tạo Project . Đầu tiên các bạn tạo thư mục có tên là helloworld-nodejs ở đâu đó trên máy tính và vào thử mục đó mở Terminal lên (CMD với Windowns) và gõ vào lệnh sau. npm init Sau đó thì nó sẽ yêu cầu các bạn nhập thông tin cho Project mới, các bạn cứ nhập theo hướng dẫn là được, ví dụ như mình nhập bên dưới .
Sau khi kết thúc bước này các bạn sẽ thấy nó tạo ra một file tên là package.json Đây chính là file chứa thông tin cài đặt của Project, về sau toàn bộ thông tin các bộ thư viện hoặc các đoạn Script cho ứng dụng đều được khai báo ở đây. Mình sẽ có một chương nói cụ thể hơn về NPM trong khóa học này . Bước 2. Cài đặt thư viện cần thiết cho Project. Từ Terminal các bạn chạy lệnh sau .
Lúc này NPM sẽ truy cập mạng và tải toàn bộ các bộ thư viện cần thiết về, nếu không có kết nối mạng, quá trình trên sẽ thấy bại. Bạn có thể phải chờ khá lâu nếu mạng chậm, máy tính mình thì cần tầm 5 phút là xong . Nếu các bạn bị lỗi : npm WARN [email protected] No repository field. Thì chỉ cần mở file package.json lên và sửa thêm vào một dòng repository như dưới đây.
Nếu không có Remote Git thì bạn cứ tạm điền vào đó là NONE cũng được, mình sẽ bày các tạo GIT project sau. Sau đó mở Editor lên tạo một file index.js ở thư mục gốc rồi gõ vào đó đoạn code sau .
Cuối cùng tại Terminal chạy lệnh
Nếu màn hình in ra dòng Hello World thì là đã thành công nhé . Tới đây là bạn đã xong bài tập đầu tiên rồi. Node.js is a platform for building fast and scalable server applications using JavaScript. Node.js is the runtime and npm is the Package Manager for Node.js modules. Visual Studio Code has support for the JavaScript and TypeScript languages out-of-the-box as well as Node.js debugging. However, to run a Node.js application, you will need to install the Node.js runtime on your machine. To get started in this walkthrough, install Node.js for your platform. The Node Package Manager is included in the Node.js distribution. You'll need to open a new terminal (command prompt) for the 0 command-line tools to be on your PATH.To test that you have Node.js installed correctly on your computer, open a new terminal and type 1 and you should see the current Node.js version installed.
Hello WorldLet's get started by creating the simplest Node.js application, "Hello World". Create an empty folder called "hello", navigate into and open VS Code:
From the File Explorer toolbar, press the New File button: and name the file 3:By using the 4 file extension, VS Code interprets this file as JavaScript and will evaluate the contents with the JavaScript language service. Refer to the VS Code JavaScript language topic to learn more about JavaScript support.Create a simple string variable in 3 and send the contents of the string to the console:
Note that when you typed 6 IntelliSense on the 7 object was automatically presented to you.Also notice that VS Code knows that 8 is a string based on the initialization to 9. If you type 0 you'll see IntelliSense showing all of the string functions available on 8.After experimenting with IntelliSense, revert any extra changes from the source code example above and save the file (⌘S (Windows, Linux Ctrl+S)). Running Hello WorldIt's simple to run 3 with Node.js. From a terminal, just type:
You should see "Hello World" output to the terminal and then Node.js returns. Integrated TerminalVS Code has an integrated terminal which you can use to run shell commands. You can run Node.js directly from there and avoid switching out of VS Code while running command-line tools. View > Terminal (⌃` (Windows, Linux Ctrl+`) with the backtick character) will open the integrated terminal and you can run 3 there:For this walkthrough, you can use either an external terminal or the VS Code integrated terminal for running the command-line tools. Debugging Hello WorldAs mentioned in the introduction, VS Code ships with a debugger for Node.js applications. Let's try debugging our simple Hello World application. To set a breakpoint in 3, put the editor cursor on the first line and press F9 or click in the editor left gutter next to the line numbers. A red circle will appear in the gutter.To start debugging, select the Run and Debug view in the Activity Bar: You can now click Debug toolbar green arrow or press F5 to launch and debug "Hello World". Your breakpoint will be hit and you can view and step through the simple application. Notice that VS Code displays a different colored Status Bar to indicate it is in Debug mode and the DEBUG CONSOLE is displayed. Now that you've seen VS Code in action with "Hello World", the next section shows using VS Code with a full-stack Node.js web app.
An Express applicationExpress is a very popular application framework for building and running Node.js applications. You can scaffold (create) a new Express application using the Express Generator tool. The Express Generator is shipped as an npm module and installed by using the npm command-line tool 0.
Install the Express Generator by running the following from a terminal:
The 8 switch installs the Express Generator globally on your machine so you can run it from anywhere.We can now scaffold a new Express application called 9 by running:
This creates a new folder called 9 with the contents of your application. The 1 parameters tell the generator to use the pug template engine.To install all of the application's dependencies (again shipped as npm modules), go to the new folder and execute 2:
At this point, we should test that our application runs. The generated Express application has a 3 file which includes a 4 script to run 5. This will start the Node.js application running.From a terminal in the Express application folder, run:
The Node.js web server will start and you can browse to http://localhost:3000 to see the running application. Great code editingClose the browser and from a terminal in the 9 folder, stop the Node.js server by pressing CTRL+C.Now launch VS Code:
The Node.js and Express documentation does a great job explaining how to build rich applications using the platform and framework. Visual Studio Code will make you more productive in developing these types of applications by providing great code editing and navigation experiences. Open the file 3 and hover over the Node.js global object 9. Notice how VS Code understands that 9 is a string. Even more interesting, you can get full IntelliSense against the Node.js framework. For example, you can require 1 and get full IntelliSense against the 1 class as you type in Visual Studio Code.VS Code uses TypeScript type declaration (typings) files (for example 3) to provide metadata to VS Code about the JavaScript based frameworks you are consuming in your application. Type declaration files are written in TypeScript so they can express the data types of parameters and functions, allowing VS Code to provide a rich IntelliSense experience. Thanks to a feature called 4, you do not have to worry about downloading these type declaration files, VS Code will install them automatically for you.You can also write code that references modules in other files. For example, in 3 we require the 6 module, which exports an 7 class. If you bring up IntelliSense on 8, you can see the shape of the 9 class.Debug your Express appYou will need to create a debugger configuration file 0 for your Express application. Click on Run and Debug in the Activity Bar (⇧⌘D (Windows, Linux Ctrl+Shift+D)) and then select the create a launch.json file link to create a default 0 file. Select the Node.js environment by ensuring that the 2 property in 3 is set to 4. When the file is first created, VS Code will look in 3 for a 4 script and will use that value as the 7 (which in this case is 8) for the Launch Program configuration.
Save the new file and make sure Launch Program is selected in the configuration dropdown at the top of the Run and Debug view. Open 3 and set a breakpoint near the top of the file where the Express app object is created by clicking in the gutter to the left of the line number. Press F5 to start debugging the application. VS Code will start the server in a new terminal and hit the breakpoint we set. From there you can inspect variables, create watches, and step through your code.Deploy your applicationIf you'd like to learn how to deploy your web application, check out the Deploying Applications to Azure tutorials where we show how to run your website in Azure. |