Reputation: 865
I know there are already different variations of the same question, however, mine is little different. I am getting this error cannot get/, this is my node js code
// set up ======================================================================
var express = require('express');
var path = require('path');
var app = express(); // create our app w/ express
var mongoose = require('mongoose'); // mongoose for mongodb
var port = process.env.PORT || 8080; // set the port
//var database = require('./config/database'); // load the database config
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var io = require('socket.io');
var messageId = {};
http = require('http'),
server = http.createServer(function (req, res) {
res.writeHead(200,{'content-type':'text/plain'});
res.write("Sever On");
res.end();
}),
io = io.listen(server);
// configuration ===============================================================
//mongoose.connect(database.localUrl); // Connect to local MongoDB instance. A remoteUrl is also available (modulus.io)
//app.use(express.static('./public')); // set the static files location /public/img will be /img for users
//app.use(morgan('dev')); // log every request to the console
//app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded
//app.use(bodyParser.json()); // parse application/json
//app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
//app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request
// routes ======================================================================
//require('./app/routes.js')(app);
// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);
My Package.json
{
"name": "node-todo",
"version": "0.0.1",
"description": "Simple todo application.",
"main": "server.js",
"author": "Scotch",
"dependencies": {
"body-parser": "^1.18.2",
"mongoose": "4.10.8",
"express": "^4.10.8",
"method-override": "^2.3.10",
"morgan": "^1.9.0",
"socket.io": "^2.0.3"
}
}
As you might have noticed, I am just trying to create a response using http.createServer(). Please let me know what did I do wrong.
Upvotes: 0
Views: 7762
Reputation: 6160
There are two servers created in your script. You only need to create any one of them.
http.createServer()
but you haven't bound any port to it. var app = express(); app.listen(port);
). Here you provided port but haven't provided any routing information.Here is what you can do :
1. If you want to use core HTTP server
replace your sever initilization with
server = http.createServer(function (req, res) {
res.writeHead(200,{'content-type':'text/plain'});
res.write("Sever On");
res.end();
}).listen(port),
2. If you want to use express
Add root route
app.get('/', function (req, res) {
res.send('hello world')
})
before app.listen(port);
Upvotes: 0
Reputation: 952
The below code just creates the server -
server = http.createServer();
But to send any response to client you must first listen to request i.e. create routes -
app.get('/', (req, res) => {
res.send('blah');
}
Other stuff you have written correctly I guess. I hope it fixes your problem.
Upvotes: 1