我是Node.js的初学者。

Express.js是什么? node。js的目的是什么? 为什么我们需要Express.js?它对我们使用Node.js有什么用处? 复述是什么?它自带Express.js吗?


当前回答

这是它力量的完美例子


router.route('/recordScore').post(async(req, res) => {   



  
  let gold_nation = req.body.gold && req.body.gold.nationality;
  let silver_nation = req.body.silver && req.body.silver.nationality;
  let bronze_nation = req.body.bronze && req.body.bronze.nationality;
  let competition_id = req.body.competition_id;

  console.log(gold_nation)
  console.log(silver_nation)
  req.body.gold && await country.updateOne({"flag" : gold_nation}, { $inc: { gold: 1 } });
  
  req.body.silver && await country.updateOne({"flag" : silver_nation}, { $inc: { silver: 1 } });
 
  req.body.bronze && await country.updateOne({"flag" : bronze_nation}, { $inc: { bronze: 1 } });
  console.log(competition_id)
  //await competition.updateOne({"_id" : competition_id}, {$set: {recorded : true}});
//!! Uncomment this and change model/competition.ts set recorer to recorded
// this is commented out so you can test adding medals for every case and not creating competitions every time
  res.status(200).json("Success");

});



  async record(){
    let index = this.competitions.findIndex(e => e._id == this.selectedCompetition);
    let goldIndex = this.competitors.findIndex(e => e._id == this.goldWinner);
    let silverIndex = this.competitors.findIndex(e => e._id == this.silverWinner);
    let bronzeIndex = this.competitors.findIndex(e => e._id == this.bronzeWinner);

    console.log(this.competitors[goldIndex]);
    console.log(this.competitors[1-goldIndex]);

    this.sportService.recordCompetition(this.competitors[goldIndex], 
                                    this.competitors[1-goldIndex],
                                    null,
                                    this.competitions[index]).subscribe((m:string) => this.reset(m))



    }

    reset(message: string){
      this.statusMessage = message;
        if(message == "Success"){
          
          this.competitions = this.competitions.filter( (c) => c._id != this.selectedCompetition);
          this.goldWinner = '';
          this.silverWinner = '';
          this.bronzeWinner = '';
        }
  
  
        setTimeout(()=>{                          
          this.statusMessage = '';
        }, 3000);
    
    }


router.route('/registerCompetitor').post(async(req, res) => {   


  //! PROVJERI DA LI JE FORMIRANJE TAKMICENJA ZAVRSENO

  let competitors = req.body.map( c => ({
    name: c.name,
    gender: c.gender,
    nationality: c.nationality,
    sport: c.sport,
    disciplines: c.disciplines
  }));
  console.log(competitors)
  await country.updateOne({"flag" : competitors[0].nationality}, { $inc: { numberOfCompetitors: competitors.length } });


  await   competitor.collection
                                    .insertMany(competitors)
                                    .then( u => {
                                      res.status(200).json("Ok")
                                    })
                                    .catch(err =>{ res.status(400).json("notOk");
                                  });
  
  
});


其他回答

ExpressJS是一个web应用程序框架,为您提供了一个简单的API来构建网站,web应用程序和后端。使用ExpressJS,你不需要担心底层协议、进程等。 Node.js的快速、简洁的web框架

Pug(早期称为Jade)是一种用于编写HTML模板的简洁语言。它−

生成HTML 支持动态代码 支持可重用性(DRY) 它是Express使用的最流行的模板语言之一。

Express.js由TJ Holowaychuk创建,现在由社区管理。它是node.js中最流行的框架之一。Express还可以用于开发各种产品,如web应用程序或RESTful API。更多信息请浏览expressjs.com官方网站。

Express.js是Node.js的模块化web框架 它用于更容易地创建web应用程序和服务 js简化了开发,更容易编写安全、模块化和快速的应用程序。你可以在普通的旧Node.js中完成所有这些,但一些bug可能(并且将会)浮出水面,包括安全问题(例如。没有正确转义字符串) Redis是一种以快速著称的内存数据库系统 的性能。不,但你可以使用redis的Express.js 客户端

我不能比这更简洁了。对于你所有其他的需求和信息,谷歌是你的朋友。

这是它力量的完美例子


router.route('/recordScore').post(async(req, res) => {   



  
  let gold_nation = req.body.gold && req.body.gold.nationality;
  let silver_nation = req.body.silver && req.body.silver.nationality;
  let bronze_nation = req.body.bronze && req.body.bronze.nationality;
  let competition_id = req.body.competition_id;

  console.log(gold_nation)
  console.log(silver_nation)
  req.body.gold && await country.updateOne({"flag" : gold_nation}, { $inc: { gold: 1 } });
  
  req.body.silver && await country.updateOne({"flag" : silver_nation}, { $inc: { silver: 1 } });
 
  req.body.bronze && await country.updateOne({"flag" : bronze_nation}, { $inc: { bronze: 1 } });
  console.log(competition_id)
  //await competition.updateOne({"_id" : competition_id}, {$set: {recorded : true}});
//!! Uncomment this and change model/competition.ts set recorer to recorded
// this is commented out so you can test adding medals for every case and not creating competitions every time
  res.status(200).json("Success");

});



  async record(){
    let index = this.competitions.findIndex(e => e._id == this.selectedCompetition);
    let goldIndex = this.competitors.findIndex(e => e._id == this.goldWinner);
    let silverIndex = this.competitors.findIndex(e => e._id == this.silverWinner);
    let bronzeIndex = this.competitors.findIndex(e => e._id == this.bronzeWinner);

    console.log(this.competitors[goldIndex]);
    console.log(this.competitors[1-goldIndex]);

    this.sportService.recordCompetition(this.competitors[goldIndex], 
                                    this.competitors[1-goldIndex],
                                    null,
                                    this.competitions[index]).subscribe((m:string) => this.reset(m))



    }

    reset(message: string){
      this.statusMessage = message;
        if(message == "Success"){
          
          this.competitions = this.competitions.filter( (c) => c._id != this.selectedCompetition);
          this.goldWinner = '';
          this.silverWinner = '';
          this.bronzeWinner = '';
        }
  
  
        setTimeout(()=>{                          
          this.statusMessage = '';
        }, 3000);
    
    }


router.route('/registerCompetitor').post(async(req, res) => {   


  //! PROVJERI DA LI JE FORMIRANJE TAKMICENJA ZAVRSENO

  let competitors = req.body.map( c => ({
    name: c.name,
    gender: c.gender,
    nationality: c.nationality,
    sport: c.sport,
    disciplines: c.disciplines
  }));
  console.log(competitors)
  await country.updateOne({"flag" : competitors[0].nationality}, { $inc: { numberOfCompetitors: competitors.length } });


  await   competitor.collection
                                    .insertMany(competitors)
                                    .then( u => {
                                      res.status(200).json("Ok")
                                    })
                                    .catch(err =>{ res.status(400).json("notOk");
                                  });
  
  
});


ExpressJS是一个基于NodeJS之上的最简单的web应用框架。

它可以用来快速构建WebApps, RESTFUL api等。

支持Jade、EJS等多种模板引擎。

ExpressJS只保留了一个极简的功能作为核心特性,因此没有orm或db作为默认支持。但只要稍加努力,expressjs应用程序就可以与不同的数据库集成。

关于创建ExpressJS应用程序的入门指南,请查看以下链接:

ExpressJS入门教程