簡體   English   中英

Node.js server.get()TypeError:未定義不是函數

[英]Node.js server.get() TypeError: undefined is not a function

var express = require('express'),
    http = require('http'),
    app = express(),
    server = http.createServer(app),
    io = require('socket.io').listen(server), //pass a http.Server instance
    fs = require('fs');

server.listen(8008);

// routing
server.get('/', function (req, res) {
  res.sendfile(__dirname + '/chat.html');
});

我有創建服務器的這段代碼。 運行此命令時,出現以下錯誤:TypeError:undefined不是函數

server.get('/', function (req, res) {
       ^
TypeError: undefined is not a function
    at Object.<anonymous> (C:\Users\rexhi\Desktop\private_mess\app.js:11:8)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

怎么了 我是Node的新手。

.get(req,res)函數屬於express的實例。 因此,您應該執行以下操作:

app.get('/', function (req, res) {
    ..
});

在我看來,您正在嘗試使用Express處理路由,如果是這種情況,則您要使用app.get()而不是server.get() 所以在你的情況下

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/chat.html');
});

看看快速路由參考以了解更多信息-http: //expressjs.com/guide/routing.html

使用app.get()代替server.get()

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/chat.html');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM