Technical terms regarding Node will be explained and linked below.
Example directory structure
So you don’t want events to interrupt the main thread of execution. This is what asynchronous means, handling events without interrupting the main thread.
Node is based on this non-blocking execution, making it one of the fastest tools for building web applications today. In the following “Hello World” example, many connections can be handled concurrently.
Upon each connection the callback is fired, but if there is no work to be done Node will remain asleep.
There are 6 simple steps in this example, bear with me. Install Node.js for your platform (MacOS, Windows or Linux).
Do you use Node…? You will.
As I am running Ubuntu on my machine, the respective version of Node.js for my operating system is listed. Go ahead, download and install it. This will give you the tools needed to run a server on your local machine.
These commands are universal for whatever OS you’ll be running. The former will create a new directory inside the directory you are currently in, mkdir = “make directory”. The latter will change into this newly created directory, cd = “change directory”.
Hard-core windows users can calm down, this will work for you guys too, as it is equivalent to creating a new folder within your file system… only more fancy. Initialize your project and link it to npm.
Now the real fun starts. After creating your directory, very innovatively named myapp, you will need to initialize a project and link it to npm.
Okay, calm down. Npm is short for node package manager. This is where all node packages live.
Packages can be viewed as bundles of code, like modules, which carry out a specific function.
This functionality is what we as developers are utilizing. We use the application program interface, the API, provided for us by these modules. What is an API you ask? The modules in turn act like black boxes with buttons and levers we can push and pull to get the desired end result.
Running this command initializes your project:. This creates a package.json file in your myapp folder. The file contains references for all npm packages you have downloaded to your project.
The command will prompt you to enter a number of things. You can enter your way through all of them EXCEPT this one:.
You will want to change this to:. Install Express in the myapp directory.
While still in the myapp directory run:. The install command will go ahead and find the package you wish to install, and install it to your project.
You will now be able to see a node_modules folder get created in the root of your project.
This is a crucial step, as you will be able to require any of the recently installed files in your own application files. The addition of —save will save the package to your dependencies list, located in the package.json, in your myapp directory.
Yeah, I know what you’re thinking. What exactly is this Express thing? Some mail delivery service, rivaling FedEx, I presume (please ignore these lame jokes). No, to burst your bubble, Express is a….
“Fast, unopinionated, minimalist web framework for Node.js” — Taken from Express.js’ official website. It gives you a set of robust and easy to use tools to get your web application up and running.
Express has become so popular, it now is the de facto standard, in the vast majority of Node.js applications today. I strongly encourage the use of Express. Start your text editor of choice and create a file named app.js.
Write the following:. Here is where you will need to use the package which was recently installed. The first line declares a variable which will contain the module called express, grabbing it from the node_modules folder.
The module is actually a function. Assigning the function call to another variable gives you access to a predefined set of tools which will in a great deal make your life much easier.
You could view the variable app as an object, whose methods you are utilizing to build the actual program. The listen method starts a server and listens on port 3000 for connections.It responds with “Hello World!” for get requests to the root URL (/).
For every other path, it will respond with a 404 Not Found. Run the appType the command:.
After running the command, load http://localhost:3000/ in a browser to see the output. You should also see “Example app listening on port 3000!” get logged to the command line. That’s it, you’re done. You have successfully created your first Node app. Don’t stop here, keep exploring the wonderful world of Node.js, as it has much more to offer. Your finished app should have a folder structure somewhat resembling this. That’s it for me today. If you liked this article and if it helped you in any way, feel free to follow me, more tutorials like this one will be coming soon.
If you believe this article will be of big help to someone, feel free to share. Happy coding :). If you liked this, click the 💚 below so other people will see this here on Medium.
StrongLoop / IBM에 의해 제공이 번역. 이 문서는 영문판 문서에 비해 더 오래된 버전일 수도 있습니다. 최신 업데이트를 확인하려면 영문판 문서를 참조하십시오.
Useful Video Courses
먼저, myapp이라는 이름의 디렉토리를 작성한 후 이 디렉토리로 이동하여 npm init를 실행하십시오. 이후 설치 안내서에 따라 express를 종속 항목으로서 설치하십시오. myapp 디렉토리에 app.js라는 이름의 파일을 작성한 후 다음과 같은 코드를 추가하십시오. 앱은 서버를 시작하며 3000번 포트에서 연결을 청취합니다. 앱은 루트 URL(/) 또는 라우트에대한 요청에 “Hello World!”로 응답합니다.
How the App Works?
다른 모든 경로에 대해서는 404 Not Found로 응답합니다. req(요청) 및 res(응답)는 Node가 제공하는 동일한 오브젝트이며, 따라서req.pipe(), req.on('data', callback) 그리고 Express의 관여가 필요 없는 다른 모든 항목을 호출할 수 있습니다. 다음의 명령을 이용하여 앱을 실행하십시오. 이후 브라우저에서 http://localhost:3000/을 로드하여 결과물을 확인하십시오.
ExpressJS Tutorial. ExpressJS Useful Resources. Selected Reading. We have set up the development, now it is time to start developing our first app using Express. Create a new file called index.js and type the following in it.