我在學習java腳本。我正在考慮使用Express,stock.io和ejs製作一個基本的聊天應用程序。無法讓它運行,Node.js,express,socket.io和ejs?
我只是不能讓它運行。
這是我app.js
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var ejs = require('ejs');
var app = express();
// view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'viwes'));
app.get('/', function(req, res) {
res.render('index');
});
io.on('connection', function(socket) {
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
我index.ejs
<!DOCTYPE html>
<html>
<head>
<title>ChatApp</title>
<link href="/css/style.css" rel="stylesheet" type="text/css" />
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
</head>
<body>
</body>
</html>
而我的package.json
"dependencies": {
"express": "4.14.0" ,
"body-parser": "1.15.2" ,
"socket.io": "1.6.0",
"connect": "3.5.0",
"ejs": "2.5.2"
},
"engines": {
"node": "4.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/heroku/node-js-sample"
},
鑽機HT現在它只是說,在瀏覽器中:
Cannot GET/
我認爲它是一個路徑問題,但我怎麼知道..
哇這就是快!謝謝,它有部分工作。現在它不能找到索引文件壽。 –
@LinusKarlsson - ''views'拼寫錯誤'path.join(__ dirname,'viwes'));' – jfriend00
哈哈謝謝!它現在有效! –