在创建我的聊天应用程序的路径中,我试图创建我的app.js文件,直到我遇到这个简单/新手愚蠢的SyntaxError,我真的不认为它有什么问题,但仍然会导致我向前发展的问题,我尝试添加额外的括号,括号或分号。但它不会自行解决:/
这是我的app.js文件:
var express = require('expres'),
//now in express you no longer create an http server automatically
//app variable is just a function that bundles together with basically everything in express
//for socket.io we need an http server object so, we need to create that manually
app = express(),
//create a variable called "server".
//requires http module, then "createServer" (creates a server) and then pass that data onto the "app" variable.
server = require('http').createServer(app),
//Then we need to create the socket functionality.
//create the variable called "io".
//The variable "io" which then requires "socket.io" and then we need to make it "listen".
//So this is why we need an http server, the parameter of "listen".
//socket.io listens to an http server object.
//So we need to pass it to "server"
io = require('socket.io').listen(server);
//Next: We actually need to tell the server itself on what port to listen on.
//Pick any available port, in this case "3000" within the option below.
server.listen(3000);
//After setting up the server
//We need to create a "route".
//Right now we can't access any pages.
//And this is where express makes somethings easier.
//Makes routing a bit easier.
//We are going to use the variable "app" . "get" function to get that variable which equals to the "express framework".
//Then we set the root directory "/" by a forward slash sign.
//So that's the first parameter on what the client is trying to access.
//And then just like without express we have a "function" with the "http request" and the "http response" as parameters.
app.get('/'), function(req, res){
//And then we declare the "http response" and . "sendfile" because, we are going to create a file called "index.html" that we want the client to get whenever it goes to "localhost:3000".
//Then create the "directory name" + the "index.html" web page.
res.sendfile(__dirname + '/index.html');
});
//Then to check if it works within command prompt execute the command: "node app.js"
//Which will run the javascript file.
这是我输入node app.js
命令时得到的结果!
我已经通过执行命令npm install socket.io
安装了socket.io,但它仍然无法解决我的问题。 :/
C:\chatapplication>npm install socket.io
npm http GET https://registry.npmjs.org/socket.io/1.4.8
npm http 304 https://registry.npmjs.org/socket.io/1.4.8
unbuild socket.io@1.4.8
npm http GET https://registry.npmjs.org/engine.io/1.6.11
npm http GET https://registry.npmjs.org/socket.io-adapter/0.4.0
npm http GET https://registry.npmjs.org/socket.io-parser/2.2.6
npm http GET https://registry.npmjs.org/has-binary/0.1.7
npm http GET https://registry.npmjs.org/debug/2.2.0
npm http GET https://registry.npmjs.org/socket.io-client/1.4.8
npm http 304 https://registry.npmjs.org/socket.io-parser/2.2.6
npm http GET https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-2.2.6.tgz
npm http 304 https://registry.npmjs.org/engine.io/1.6.11
npm http 304 https://registry.npmjs.org/debug/2.2.0
npm http 304 https://registry.npmjs.org/socket.io-adapter/0.4.0
npm http 304 https://registry.npmjs.org/has-binary/0.1.7
npm http 304 https://registry.npmjs.org/socket.io-client/1.4.8
npm http GET https://registry.npmjs.org/engine.io/-/engine.io-1.6.11.tgz
npm http GET https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-0.4.0.tgz
npm http GET https://registry.npmjs.org/socket.io-client/-/socket.io-client-1.4.8.tgz
npm http GET https://registry.npmjs.org/has-binary/-/has-binary-0.1.7.tgz
npm http 200 https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-2.2.6.tgz
npm http 200 https://registry.npmjs.org/engine.io/-/engine.io-1.6.11.tgz
npm http 200 https://registry.npmjs.org/has-binary/-/has-binary-0.1.7.tgz
npm http 200 https://registry.npmjs.org/socket.io-client/-/socket.io-client-1.4.8.tgz
npm http 200 https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-0.4.0.tgz
npm http GET https://registry.npmjs.org/isarray/0.0.1
npm http GET https://registry.npmjs.org/socket.io-parser/2.2.2
npm http GET https://registry.npmjs.org/ms/0.7.1
npm http GET https://registry.npmjs.org/json3/3.3.2
npm http GET https://registry.npmjs.org/component-emitter/1.1.2
npm http GET https://registry.npmjs.org/benchmark/1.0.0
npm http GET https://registry.npmjs.org/base64id/0.1.0
npm http GET https://registry.npmjs.org/engine.io-parser/1.2.4
npm http GET https://registry.npmjs.org/ws/1.1.0
npm http GET https://registry.npmjs.org/accepts/1.1.4
npm http 304 https://registry.npmjs.org/socket.io-parser/2.2.2
npm http 304 https://registry.npmjs.org/isarray/0.0.1
npm http GET https://registry.npmjs.org/engine.io-client/1.6.11
npm http GET https://registry.npmjs.org/component-emitter/1.2.0
npm http GET https://registry.npmjs.org/component-bind/1.0.0
npm http GET https://registry.npmjs.org/indexof/0.0.1
npm http GET https://registry.npmjs.org/object-component/0.0.3
npm http GET https://registry.npmjs.org/to-array/0.1.4
npm http GET https://registry.npmjs.org/parseuri/0.0.4
npm http 304 https://registry.npmjs.org/json3/3.3.2
npm http 304 https://registry.npmjs.org/ms/0.7.1
npm http GET https://registry.npmjs.org/backo2/1.0.2
npm http 304 https://registry.npmjs.org/benchmark/1.0.0
npm http 304 https://registry.npmjs.org/base64id/0.1.0
npm http 304 https://registry.npmjs.org/component-emitter/1.1.2
npm http GET https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-2.2.2.tgz
npm http 304 https://registry.npmjs.org/engine.io-parser/1.2.4
npm http 304 https://registry.npmjs.org/ws/1.1.0
npm http GET https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz
npm http 304 https://registry.npmjs.org/engine.io-client/1.6.11
npm http 304 https://registry.npmjs.org/component-emitter/1.2.0
npm http 304 https://registry.npmjs.org/accepts/1.1.4
npm http 304 https://registry.npmjs.org/component-bind/1.0.0
npm http 304 https://registry.npmjs.org/indexof/0.0.1
npm http GET https://registry.npmjs.org/base64id/-/base64id-0.1.0.tgz
npm http 304 https://registry.npmjs.org/object-component/0.0.3
npm http GET https://registry.npmjs.org/component-emitter/-/component-emitter-1.1.2.tgz
npm http GET https://registry.npmjs.org/json3/-/json3-3.3.2.tgz
npm http GET https://registry.npmjs.org/ms/-/ms-0.7.1.tgz
npm http GET https://registry.npmjs.org/benchmark/-/benchmark-1.0.0.tgz
npm http 304 https://registry.npmjs.org/to-array/0.1.4
npm http 304 https://registry.npmjs.org/parseuri/0.0.4
npm http 304 https://registry.npmjs.org/backo2/1.0.2
npm http GET https://registry.npmjs.org/ws/-/ws-1.1.0.tgz
npm http GET https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-1.2.4.tgz
npm http GET https://registry.npmjs.org/engine.io-client/-/engine.io-client-1.6.11.tgz
npm http GET https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.0.tgz
npm http GET https://registry.npmjs.org/accepts/-/accepts-1.1.4.tgz
npm http GET https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz
npm http GET https://registry.npmjs.org/indexof/-/indexof-0.0.1.tgz
npm http GET https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz
npm http GET https://registry.npmjs.org/to-array/-/to-array-0.1.4.tgz
npm http GET https://registry.npmjs.org/parseuri/-/parseuri-0.0.4.tgz
npm http 200 https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-2.2.2.tgz
npm http 200 https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz
npm http 200 https://registry.npmjs.org/ms/-/ms-0.7.1.tgz
npm http 200 https://registry.npmjs.org/base64id/-/base64id-0.1.0.tgz
npm http 200 https://registry.npmjs.org/benchmark/-/benchmark-1.0.0.tgz
npm http 200 https://registry.npmjs.org/component-emitter/-/component-emitter-1.1.2.tgz
npm http 200 https://registry.npmjs.org/json3/-/json3-3.3.2.tgz
npm http 200 https://registry.npmjs.org/ws/-/ws-1.1.0.tgz
npm http 200 https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-1.2.4.tgz
npm http 200 https://registry.npmjs.org/engine.io-client/-/engine.io-client-1.6.11.tgz
npm http 200 https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.0.tgz
npm http 200 https://registry.npmjs.org/accepts/-/accepts-1.1.4.tgz
npm http 200 https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz
npm http 200 https://registry.npmjs.org/indexof/-/indexof-0.0.1.tgz
npm http 200 https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz
npm http 200 https://registry.npmjs.org/to-array/-/to-array-0.1.4.tgz
npm http 200 https://registry.npmjs.org/parseuri/-/parseuri-0.0.4.tgz
npm http GET https://registry.npmjs.org/json3/3.2.6
npm http GET https://registry.npmjs.org/debug/0.7.4
npm http GET https://registry.npmjs.org/component-emitter/-/component-emitter-1.1.2.tgz
npm http 304 https://registry.npmjs.org/json3/3.2.6
npm http 304 https://registry.npmjs.org/debug/0.7.4
npm http GET https://registry.npmjs.org/json3/-/json3-3.2.6.tgz
npm http 200 https://registry.npmjs.org/component-emitter/-/component-emitter-1.1.2.tgz
npm http GET https://registry.npmjs.org/debug/-/debug-0.7.4.tgz
npm http 200 https://registry.npmjs.org/json3/-/json3-3.2.6.tgz
npm http GET https://registry.npmjs.org/mime-types
npm http GET https://registry.npmjs.org/negotiator/0.4.9
npm http 304 https://registry.npmjs.org/negotiator/0.4.9
npm http 200 https://registry.npmjs.org/debug/-/debug-0.7.4.tgz
npm http GET https://registry.npmjs.org/after/0.8.1
npm http GET https://registry.npmjs.org/arraybuffer.slice/0.0.6
npm http GET https://registry.npmjs.org/base64-arraybuffer/0.1.2
npm http GET https://registry.npmjs.org/has-binary/0.1.6
npm http GET https://registry.npmjs.org/blob/0.0.4
npm http GET https://registry.npmjs.org/utf8/2.1.0
npm http GET https://registry.npmjs.org/negotiator/-/negotiator-0.4.9.tgz
npm http 304 https://registry.npmjs.org/mime-types
npm http 304 https://registry.npmjs.org/blob/0.0.4
npm http GET https://registry.npmjs.org/blob/-/blob-0.0.4.tgz
npm http 304 https://registry.npmjs.org/utf8/2.1.0
npm http GET https://registry.npmjs.org/options
npm http GET https://registry.npmjs.org/ultron
npm http 304 https://registry.npmjs.org/arraybuffer.slice/0.0.6
npm http 304 https://registry.npmjs.org/after/0.8.1
npm http 304 https://registry.npmjs.org/base64-arraybuffer/0.1.2
npm http 304 https://registry.npmjs.org/has-binary/0.1.6
npm http GET https://registry.npmjs.org/utf8/-/utf8-2.1.0.tgz
npm http 200 https://registry.npmjs.org/negotiator/-/negotiator-0.4.9.tgz
npm http 304 https://registry.npmjs.org/ultron
npm http GET https://registry.npmjs.org/arraybuffer.slice/-/arraybuffer.slice-0.0.6.tgz
npm http GET https://registry.npmjs.org/after/-/after-0.8.1.tgz
npm http GET https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.2.tgz
npm http GET https://registry.npmjs.org/has-binary/-/has-binary-0.1.6.tgz
npm http 200 https://registry.npmjs.org/blob/-/blob-0.0.4.tgz
npm http 200 https://registry.npmjs.org/has-binary/-/has-binary-0.1.6.tgz
npm http 200 https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.2.tgz
npm http 200 https://registry.npmjs.org/utf8/-/utf8-2.1.0.tgz
npm http 200 https://registry.npmjs.org/after/-/after-0.8.1.tgz
npm http 304 https://registry.npmjs.org/options
npm http GET https://registry.npmjs.org/better-assert
npm http 200 https://registry.npmjs.org/arraybuffer.slice/-/arraybuffer.slice-0.0.6.tgz
npm http 304 https://registry.npmjs.org/better-assert
npm http GET https://registry.npmjs.org/callsite/1.0.0
npm http 304 https://registry.npmjs.org/callsite/1.0.0
npm http GET https://registry.npmjs.org/mime-db
npm http GET https://registry.npmjs.org/has-cors/1.1.0
npm http GET https://registry.npmjs.org/ws/1.0.1
npm http GET https://registry.npmjs.org/xmlhttprequest-ssl/1.5.1
npm http GET https://registry.npmjs.org/parsejson/0.0.1
npm http GET https://registry.npmjs.org/parseqs/0.0.2
npm http GET https://registry.npmjs.org/component-inherit/0.0.3
npm http GET https://registry.npmjs.org/yeast/0.1.2
npm http GET https://registry.npmjs.org/component-emitter/-/component-emitter-1.1.2.tgz
npm http GET https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-1.2.4.tgz
npm http 304 https://registry.npmjs.org/mime-db
npm http 304 https://registry.npmjs.org/parseqs/0.0.2
npm http 304 https://registry.npmjs.org/component-inherit/0.0.3
npm http GET https://registry.npmjs.org/parseqs/-/parseqs-0.0.2.tgz
npm http 304 https://registry.npmjs.org/ws/1.0.1
npm http 304 https://registry.npmjs.org/has-cors/1.1.0
npm http GET https://registry.npmjs.org/component-inherit/-/component-inherit-0.0.3.tgz
npm http 304 https://registry.npmjs.org/yeast/0.1.2
npm http 304 https://registry.npmjs.org/xmlhttprequest-ssl/1.5.1
npm http 200 https://registry.npmjs.org/component-emitter/-/component-emitter-1.1.2.tgz
npm http 200 https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-1.2.4.tgz
npm http GET https://registry.npmjs.org/has-cors/-/has-cors-1.1.0.tgz
npm http GET https://registry.npmjs.org/ws/-/ws-1.0.1.tgz
npm http GET https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.1.tgz
npm http GET https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz
npm http 200 https://registry.npmjs.org/parseqs/-/parseqs-0.0.2.tgz
npm http 200 https://registry.npmjs.org/component-inherit/-/component-inherit-0.0.3.tgz
npm http 200 https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz
npm http 200 https://registry.npmjs.org/has-cors/-/has-cors-1.1.0.tgz
npm http 200 https://registry.npmjs.org/ws/-/ws-1.0.1.tgz
npm http 200 https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.1.tgz
npm http 304 https://registry.npmjs.org/parsejson/0.0.1
npm http GET https://registry.npmjs.org/callsite/-/callsite-1.0.0.tgz
npm http GET https://registry.npmjs.org/after/-/after-0.8.1.tgz
npm http GET https://registry.npmjs.org/arraybuffer.slice/-/arraybuffer.slice-0.0.6.tgz
npm http GET https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.2.tgz
npm http GET https://registry.npmjs.org/has-binary/-/has-binary-0.1.6.tgz
npm http GET https://registry.npmjs.org/blob/-/blob-0.0.4.tgz
npm http 200 https://registry.npmjs.org/callsite/-/callsite-1.0.0.tgz
npm http 200 https://registry.npmjs.org/arraybuffer.slice/-/arraybuffer.slice-0.0.6.tgz
npm http 200 https://registry.npmjs.org/after/-/after-0.8.1.tgz
npm http 200 https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.2.tgz
npm http 200 https://registry.npmjs.org/has-binary/-/has-binary-0.1.6.tgz
npm http 200 https://registry.npmjs.org/blob/-/blob-0.0.4.tgz
socket.io@1.4.8 node_modules\socket.io
├── has-binary@0.1.7 (isarray@0.0.1)
├── debug@2.2.0 (ms@0.7.1)
├── socket.io-parser@2.2.6 (isarray@0.0.1, component-emitter@1.1.2, json3@3.3.2, benchmark@1.0.0)
├── engine.io@1.6.11 (base64id@0.1.0, ws@1.1.0, engine.io-parser@1.2.4, accepts@1.1.4)
├── socket.io-adapter@0.4.0 (socket.io-parser@2.2.2)
└── socket.io-client@1.4.8 (component-emitter@1.2.0, to-array@0.1.4, indexof@0.0.1, component-bind@1.0.0, backo2@1.0.2, object-component@0.0.3, parseuri@0.0.4, engine.io-client@1.6.11)
C:\chatapplication>node app.js
C:\chatapplication\app.js:45
});
^
SyntaxError: Unexpected token )
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
C:\chatapplication>
我有什么想法可以解决这个问题,因为在我看来,我似乎没有任何语法错误:/ /
非常感谢。
答案 0 :(得分:1)
app.get结构是:
app.get(path, callback [, callback ...])
正确的代码是:
app.get('/', function(req, res){
//And then we declare the "http response" and . "sendfile" because, we are going to create a file called "index.html" that we want the client to get whenever it goes to "localhost:3000".
//Then create the "directory name" + the "index.html" web page.
res.sendfile(__dirname + '/index.html');
});
有关快速路由的更多信息here。
答案 1 :(得分:-1)
这里有一个额外的结束括号
app.get('/'),function(req,res){