


The purpose of this time is to build a most basic NodeJS server that can realize functions and reflect the workflow of NodeJS and the basic framework of development.
Requirements: nodejs and express have been installed.
1. Build a basic NodeJS server (express, routing)
var express = require(‘express'); //引入express模块 var app = express(); //调用express()函数,对函数进行初始化 app.get('/stooges/:name?', function(req, res, next){ //设置第一个路由,期待一个name被输入 var name = req.params.name; //获取输入的名字,req.params switch(name?name.toLowerCase():' '){ //对名字进行判断 case ‘larry': case ‘curly': case ‘moe': res.send(name + 'is my favorite stooge.'); //符合条件利用res.send发送信息 break; default: next(); //next()函数,在function中也有参数传递,它的含义是如果这个路由传递的参数不够不能执行这个路由的话,next()函数表示跳转到下一个函数继续执行(这里是路由) } }); app.get(‘/stooges/*?', function(){ //这里?表示最后的参数可以有也可以没有,同上一个路由相同 res.send(‘no stooges listed'); }); app.get(‘/?', function(req,res){ //什么都没有的时候默认的路由 res.send(‘hello world'); }); var port = 8080; //设定并对端口进行监听 app.listen(port); console.log(‘Listensing on port' + port);
2. Use Jade template engine and add template rendering
var express = require(‘express'); var app = express(); //下面三句话完成了对view的设定,包括引擎、模板路径以及其他设定 app.set(‘view engine', ‘jade'); app.set(‘view options', {layout:true}); app.set(‘views', __dirname + ‘/views'); app.get('/stooges/:name?', function(req, res, next){ var name = req.params.name; switch(name?name.toLowerCase():' '){ case ‘larry': case ‘curly': case ‘moe': res.render(‘stooges', {stooge: name}); //进行视图的渲染,传入模板名即可 break; default: next(); } }); app.get(‘/stooges/*?', function(req, res){ res.render(‘stooges', {stooges:null}); }); app.get(‘/?', function(req, res){ res.render(‘index'); }); var port = 8080; app.listen(port); console.log(‘Listensing on port' + port);
There are three template files in total, namely layout.jade (layout file), index.jade and stooges.jade. The codes of the three template files are as follows:
layout.jade
!!! 5 //代表文档类型是HTML5 html(lang=”en”) head title My Web Site block scripts block content
index.jade
entends layout block content hi hello world
stooges.jade
extends layout block content if(stooge) p #{stooge} is my favorite stooge. //这里的#{stooge}获取了js渲染模板时传递进来的参数 Else p no stooges listed
With the above code, you can use node.js and express to build a basic node application.

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!
