One 、 Introduce
Nodejs Is based on Google V8 Engine Javascript Running environment ;
It makes JavaScript You can develop back-end programs , It can achieve almost all the functions that other back-end languages can achieve ;
Nodejs What I'm good at is dealing with high concurrency ;
stay Java、PHP perhaps .net Etc. in the server-side language , A new thread will be created for each client connection , And each thread costs about 2MB Memory . in other words , Theoretically , One 8GB The maximum number of users that a memory server can connect to at the same time is 4000 about . Must let Web Applications support more users , You need to increase the number of servers , Web Of course, the hardware cost of the application goes up .Nodejs Do not create a new thread for each client connection , And just one thread , When there are users connected , To trigger an internal event , Through non blocking I/O、 Event driven mechanism , Make it lightweight and efficient , One 8GB Memory servers , Can handle more than at the same time 4 Million user connections .
Nodejs With a powerful and flexible package manager (node package manager,npm);
There are already tens of thousands of third-party modules , There is a website development framework , Yes MySQL、MongoDB Database interface , There are template language parsing 、CSS Generation tool 、 mail 、 encryption 、 graphics 、 Debugging support , There are even graphical user interfaces and operating systems API Tools .
Two 、 Why learn Nodejs
- Large users : We can't count Nodejs Software downloads , But we can pass Nodejs frame Express To analyze the download amount of Nodejs The number of users is amazing .
Simple :Nodejs Grammar is all about it js grammar , As long as you know JS You can learn the basics Nodejs The backend development , bring Nodejs It has a short development cycle 、 Low development cost 、 The advantages of low learning cost .
Node.js Medium JavaScript It's just Core JavaScript, Or rather, ECMAScript An implementation of , It doesn't contain DOM、BOM perhaps Client JavaScript. This is because Node.js Not running in browser , So you don't need to use many of the features in the browser , There is no such thing as JavaScript Browser compatibility issues , You can use JavaScript All the characteristics of language .
3、 ... and 、 install
Nodejs The installation is the same as normal software , Official website Download the latest version , Proposed installation Current edition ,LTS There's something new in the version API Can't use , Just go to the next step , After installation , Open the console to run ’node -v‘ Check whether the installation is successful , here npm It's already installed .
Sometimes you may want to try some interesting features in a new version , But also want to maintain a relatively stable environment . Based on this demand ,Node.js The community developed a multi version Manager , Used to maintain multiple versions of... On a single machine Node.js example , Easy to switch on demand . There are many different implementations right now ,https://github.com/creationix/nvm perhaps https://github.com/visionmedia/n.
Proposed installation nvm, Manage different versions of node and npm
1.mac Refer to the link for the installation method of https://segmentfault.com/a/11...
2.windows Direct installation nvm-windows that will do ( Be sure to uninstall the installed before installation Nodejs, Otherwise there will be conflict )
Four 、 Start a simple Nodejs The server
Create a new one app.js file , On the command line through node app.js, You can run a server , Access... In browser access http://127.0.0.1:3000/, You can see that Hello Nodejs.
const host = '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 Nodejs');})
server.listen(port, host, () => {
console.log(` The server is running at http://${host}:${port}/`)})
Full sample code address :https://github.com/wanwan0306/future/tree/main/Demo/NodeJs/1 initial nodejs