Nodejs / Express - Launching my app: express.createServer() is deprecated

前端 未结 5 1059
北荒
北荒 2020-12-05 02:55

I downloaded a node app to test and play around with. I have googled around and found that Express is found to be a little outdated. Can someone help me to fix the implement

相关标签:
5条回答
  • 2020-12-05 03:17

    I am already using the following snippet and it is working fine:

    var express =require("express");
    var http = require("http");
    
    var app = express();
    //app routers here 
    ...
    var httpServer = http.Server(app);
    
    httpServer.listen({PORT}, function(err){
    
    });
    
    0 讨论(0)
  • 2020-12-05 03:21
    var express = require('express');
    var app = express();
    app.listen(your_port_number);
    

    With the newer release of express (express 4.x), you do not need to create server. app.listen internally does that. Refer https://expressjs.com/en/4x/api.html#app.listen

    0 讨论(0)
  • 2020-12-05 03:30

    The solution is given in the error.

    Warning: express.createServer() is deprecated, express applications no longer inherit from http.Server please use:

      var express = require("express");
      var app = express();
    

    So you will have to just do this.

    var express = require('express')
      , http = require('http');
    
    var app = express(); 
    var server = http.createServer(app);
    
    0 讨论(0)
  • 2020-12-05 03:36

    Another potential solution to this is to install express 2.5.8 as a dependency.

    Add to package.json:

        {
        "name": "authentication"
      , "version": "0.0.1"
      , "private": true
      , "dependencies": {
          "express": "2.5.8"
        , "jade": ">= 0.26.1"
      }
    }
    

    and then run

    npm install
    
    0 讨论(0)
  • 2020-12-05 03:40

    Its can done with this simple program:

    var express = require('express');
    var app = express();
    app.get('/',
        function(req,res)
        {
            res.send("express");
        }
    );
    app.listen(3333);
    

    it works fine.

    0 讨论(0)
提交回复
热议问题