I"m getting this message when I run mongod.
In the tutorial I'm doing, I think I should be getting something like "connection accepted from 127.0.0.16:6.."
I checked out this post already - mongod HostnameCanonicalizationWorker error on OS X
and I have my hosts file set up as such:
127.0.0.1 localhost
127.0.0.1 Bens-MacBook-Pro.local
255.255.255.255 broadcasthost
::1 localhost
These are my scripts for Node:
// Main starting point of the application
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const app = express();
const morgan = require('morgan');
const router = require('./router');
const mongoose = require('mongoose');
// DB Setup
mongoose.connect('mongodb://localhost:auth/auth');
// App Setup
app.use(morgan('combined'));
app.use(bodyParser.json({type: '*/*' }));
router(app);
// Server Setup
const port = process.env.PORT || 3090;
const server = http.createServer(app);
server.listen(port);
console.log('Server listening on:', port);
This is the log for mongod:
2016-11-20T16:33:13.095-0700 I CONTROL [initandlisten] MongoDB starting : pid=15054 port=27017 dbpath=/data/db 64-bit host=Bens-MacBook-Pro.local
......
2016-11-20T16:33:13.380-0700 I NETWORK [initandlisten] waiting for connections on port 27017
What is missing?
Thanks!
UPDATED
http://code.runnable.com/UWxv-JS8trEHAACH/connect-to-mongodb-using-mongoosejs-for-node-js
I added a listener to check for connection:
/*
* More details here http://mongoosejs.com/docs/index.html
*/
//require mongoose node module
var mongoose = require('mongoose');
//connect to local mongodb database
var db = mongoose.connect('mongodb://127.0.0.1:27017/test');
//attach lister to connected event
mongoose.connection.once('connected', function() {
console.log("Connected to database")
});
And it is logging "Connected to database" so it appears to be connected even thought the mongod window says it's not. Let me go through the rest of the tutorial and see if it's truly connected.
You are making some errors
mongoose.connect('mongodb://localhost:auth/auth'); is looking for a port :auth that does not exist
const server = http.createServer(app); you can simply do app.listen(port);
router(app); you can simply do require('yourRouteFile.js')(app);
You should get it going with the following server.js
// Main starting point of the application
const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const app = express();
const morgan = require('morgan');
const mongoose = require('mongoose');
const port = process.env.PORT || 3090;
// DB Setup
mongoose.connect('mongodb://localhost/auth');
// App Setup
app.use(morgan('combined'));
app.use(bodyParser.json({type: '*/*' }));
//Routes
require('yourRouteFile.js')(app);
// Server Setup
app.listen(port);
console.log('Server listening on:', port);
Ok. So i followed the rest of the node + mongoose tutorial and using postman, I was able to save stuff to the db, verified by Robomongo. But my log with mongod still says "waiting for connections on port 27017". So I am not sure why.....But things work.
Related
I have an issue I can't resolve for a while now. If anyone has a clue, or could help me via TeamViewer, i'll pay reward of 100$. I need this working Monday.
So. I have a webserver set up on 46.231.206.172
There I initially installed an apache2 webserver to host my website under the domain magnolian.ch
Then I switched to a nodejs server though. So I uninstalled apache2 and instead, install a service for the nodejs server.js file.
This worked great on Port 80.
But now all of a sudden (Dont know why, even back 2 weeks of backups still error...).
When I try to load the site, it times out.
When I manually want to start the server via the command "node server.js" it gives the error:
Adress already in use on port 80.
So i have no clue what I need to do here. I cant find any process with Port 80.
lsof -i :80 returns nothing....
Theres no service running that I can seem to find out...
Any ideas?
Much love
Roger, let me know!
Discord for support: Hunter S. Thompson#0275
Code:
Server.js
const http = require('http');
const app = require('./app');
const server = http.createServer(app);
server.listen(80);
App.js
const express = require('express');
const bodyparser = require('body-parser');
const offerRoutes = require('./api/routes/offer');
const contactRoutes = require('./api/routes/contacts');
const userRoutes = require('./api/routes/users');
const aboutRoutes = require('./api/routes/abouts');
const servicesRoutes = require('./api/routes/services');
const newsRoutes = require('./api/routes/news');
const homeRoutes = require('./api/routes/home');
const technoRoutes = require('./api/routes/techno');
const errorRoutes = require('./api/routes/error');
const app = express();
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.json());
app.set('view engine', 'ejs');
app.set('views', './api/views');
app.use(express.static(__dirname + '/public'));
app.use('/offer', offerRoutes);
app.use('/about', aboutRoutes);
app.use('/services', servicesRoutes);
app.use('/news', newsRoutes);
app.use('/techno', technoRoutes);
app.use('/contact', contactRoutes);
app.use('/user', userRoutes);
app.use('/', homeRoutes);
app.use('/*', errorRoutes);
app.listen(80, '127.0.0.1');
module.exports = app;
I'm learning Js.
I have a problem in the code:
const express = require('express');
const app = express();
const PORT = 3000;
app.listen(PORT, function(){
console.log("The Express is running");
});
app.get('/', (req,res)=>{
res.send('Ok');
});
running node app.js i have the answer "Ok".But, running in the browser(localhost:3000) i get : "The connection was refused".
necessary info: I'm use a container docker, which I use the ubuntu. Can this cause a problem?
I am a newbie to node.js and was trying to create a sample web server with the code below but I got this error instead. could someone please point out/ explain what went wrong?
req.next = next;
^
TypeError: Cannot create property 'next' on number '8080'
// Setup empty JS object to act as endpoint for all routes
projectData = {};
// Require Express to run server and routes
const express = require('express');
// Start up an instance of app
const app = express();
/* Middleware*/
//Here we are configuring express to use body-parser as middle-ware.
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Cors for cross origin allowance
const cors = require('cors');
app.use(cors());
// Initialize the main project folder
app.use(express.static('Weather-Journal-App'));
// Setup Server
const port = 8080;
const server = app(port, listening)
function listening(){
console.log(`App server is up and running on localhost: port ${port}`);
};
I believe you want to use:
app.listen(port, listening)
instead of app(port, listenting)
When I start up a basic express.js server I usually console.log a message that says it is running and listening on port .
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('App reached.'));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
However, I would like to add to this message the available IP address exposing the app to other machines on the network. Somewhat like create-react-app basic template does. How can i reach that info with my javascript code?
Has indeed already been answered:
Get local IP address in node.js
I preferred this solution:
npm install ip
const express = require('express');
const ip = require('ip');
const ipAddress = ip.address();
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('App reached.'));
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
console.log(`Network access via: ${ipAddress}:${port}!`);
});
I am writing a RESTful API. IT runs on node.js using the express.js framework, mongodb using mongoose as the object modelling tool & body-parser to pass the http. Everytime I start the server & navigate to the specified IP address, I get a "CANNOT GET/" error. How can I can around this? Some advice would be much appreciated .
I have tired using a different port number but the problem still persists.
Here is a copy of my server.js code:
var express = require('express'),
app = express(),
IP = process.env.IP,
port = process.env.PORT || 8080 ,
mongoose = require('mongoose'),
tasks = require('./api/models/todosModel'),
bodyParser = require('body-parser');
//handiling of promise
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Todosdb',{ useNewUrlParser: true });
app.use(bodyParser.urlencoded({extended:true})); // telling the sever instance to use body parser
app.use(bodyParser.json());
var Routes = require('./api/routes/todoRoutes');
//passing the server instance to the routes
Routes(app);
app.listen(port,IP);
console.log("The TODO API server is running on IP: " + IP + " and port: " + port);
The todoRoute code :
'use strict';
module.exports = function(app){
var todofunctions = require('../controllers/todoController');
// todo routes
app.route('/tasks') //task [GET (all the tasks),POST]
.get(todofunctions.listTasks)
.post(todofunctions.createTask);
app.route('/tasks/:taskId') //a task [GET(single task),PUT,DELETE]
.put(todofunctions.updatetask)
.get(todofunctions.readTask)
.delete(todofunctions.deleteTask);
};
It's probably because you have not defined any handler for /.
Try going to the /tasks instead in your browser, then you will get some response.