how to check if i have mongodb installed on my computer - node.js

I'm trying to create a RESTful API using Node.js and MongoDB for the first time. I'm new to back end programming.
For some reason am getting this error
module.js:327
throw err;
^
Error: Cannot find module './routes/api'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\xampp\htdocs\rest\server.js:15:17)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
[nodemon] app crashed - waiting for file changes before starting...
I am unsure whether MongoDB is installed correctly, but I don't know how to check that.
Please help me, either with the error or how I can to check that I have installed MongoDB correctly.
Here is my server.js
// DEPENDENCIES
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
// MongoDB
mongoose.connect('mongodb://localhost/rest_test');
// EXPRESS
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// ROUTES
app.use('/api', require('./routes/api'));
// START SERVER
app.listen(3000);
console.log('API is working on port 3000')

First you need to run MongoDB on your machine use command mongod (if you have a path to mongod.exe in your System PATH Variable)
Install all dependencies for your project with npm install express mongoose body-parser
And Error: Cannot find module './routes/api' Where your api.js file ?
After that you can run your server use node server.js
Since I do not see your file api.js I write it myself for clarity
server.js
// DEPENDENCIES
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
// MongoDB
mongoose.connect('mongodb://localhost/rest_test');
// EXPRESS
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// ROUTES
app.use('/api', require('./routes/api'));
// START SERVER
app.listen(3000);
console.log('API is working on port 3000')
routes/api.js
module.exports = function(app, res) {
res.sendFile(process.cwd() + '/routes/index.html');
}
routes/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>Hello Node.js</h1>
</body>
</html>

Assuming that you are on Windows:
Check in your program files in C:\ to look for a Mongo folder
To use mongo, you need to start a server on your machine.
To do this, launch mongod.exe in the mongo folder you have found previously.
It should work after the server is started!

Related

node server.js not responding error message

I am working on a project that requires the use of node server.js file. The documentation for the project states to type in 'node server.js' in terminal to start the server, but when I do that I'm receiving an error cannot find module 'express'.
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/.../Desktop/react/react-app-project/server.js:6:15)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
This is the code in server.js
var express = require('express');
var app = express();
/*
this says: serve all the files in the src directory if they match the URL
For example, if the client requests http://server/css/app.css then the file in src/css/app.css will be served
But if the client requests http://server/step-2 then since there is no file by that name the middleware will call next()
*/
app.use(express.static(__dirname + '/src'));
/* insert any app.get or app.post you need here. only if you do the advanced part */
/*
This says: for any path NOT served by the middleware above, send the file called index.html instead.
For example, if the client requests http://server/step-2 the server will send the file index.html. Then on the browser, React Router will load the appropriate component
*/
app.get('/*', function(request, response) {
response.sendFile(__dirname + '/src/index.html');
});
app.listen(process.env.PORT || 8080, function() {
console.log('server started');
});
Cannot find module 'express'
the reason is that you didn't install express yet.
solution: npm install express

error in socket.io code execution

From the Digital Ocean Community at this url, https://www.digitalocean.com/community/tutorials/how-to-install-express-a-node-js-framework-and-set-up-socket-io-on-a-vps, I encounter an eroor when trying to execute Part 4: The Server Code. The article states "Go and open up the app.js file in the Express application folder. Inside you'll a bunch of auto-generated code, delete all of it and use the following example instead:"
Here is the code:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http');
var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server); // this tells socket.io to use `our express server`
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
console.log("Express server listening on port 3000");
When I make these changes to app.js and then try to execute the app I get the following error:
/root/socketio-test/app.js:9
var app = express();
^
TypeError: object is not a function
at Object.<anonymous> (/root/socketio-test/app.js:9:11)
at Module._compile (module.js:456:26)
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:902:3
I have no idea how to correct this error.
System info: Ubuntu LEMP on 14.04
node.js installed, express installed, socket.io installed.
I believe you have some how installed the wrong version of express..
Do a express --version before express 3.x express was not a function.
I tried to install npm install express -g on a new droplet and I cannot get express to be an executable.
As far as I understand the express skeleton generator was moved into a separate package which can be installed using npm install express-generator -g
Which will give you the command line executable express, after you install express-generator you should be good to go.
http://expressjs.com/en/starter/generator.html
I think DO needs to update their tutorial.

how to install new nodejs modules to expressjs project?

Currently I am working on online yaml code editor. I use expressjs framework for it. I want to install yamljs module to it. How do I install it? Because following code line cause to an error.
var YAML = require('yamljs');
This is the code segment
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var YAML = require('yamljs');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
This is the error
Error: Cannot find module 'yamljs'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/tharindu/Documents/projects/Group project2/CuubezFinal/app.js:12:12)
at Module._compile (module.js:456:26)
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 Module.require (module.js:364:17)
I use webstorm for coding. I have installed yamljs module using
npm install yamljs
This library is perfectly using if I run my code manually from the ubuntu terminal. But When I run the expressjs project, It gives above error.
could you provide more information like the error text?
Have you tried
npm install yamljs
to make the module accessible to the node environment?
This library is perfectly using if I run my code manually from the
ubuntu terminal. But When I run the expressjs project, It gives above
error.
Make sure your node_modules folder is available in the same or a more outer directory than the folder of your executing script.

nodejs connect cannot find static

NOTE: I have tried other solution given here but it didn't work
A newbie with NodeJs. I am trying to follow AngularJS pro and got stuck with setting up NodeJs server. According to book, I installed nodejs and then installed connect package using
npm install connect
then downloaded angularjs in folder next to nodejs folder. Then wrote server.js file to connect to server. Here is the content of the file:
var connect = require('connect');
connect.createServer(connect.static("../angularjs")).listen( 5000);
When I run this server.js file using:
node server.js
I get following error:
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
has no method 'static'
at Object.<anonymous> (C:\web\nodejs\server.js:2:36)
at Module._compile (module.js:456:26)
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:906:3
Any ideas guys?
Thanks.
The connect package has made some changes in the latest 3.x version of their code base, moving the static middleware to it's own package. You can view the list of packages that have been moved here.
So you have two options:
Option 1
You can install an older, 2.x version of connect and use that as is:
$ npm install connect#2.X.X
Installing the latest 2.X.X version will allow your current implementation to function properly.
Option 2
You can continue to use the 3.x version of connect, and also add serve-static:
$ npm install serve-static
You would also have to update your server.js file to include the new serve-static module:
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("../angularjs"));
app.listen(5000);
The answer by dylants is helpful. However, here are the exact steps that I followed in order to resolve the same error.
1. From the command window , change directory to the directory in which you installed nodeJS.
2. After having already ran npm install connect, run:
npm install serve-static
3. Create a file named server.js with the following code:
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("./angularjs"));
app.listen(5000);
While still in command window, and still in the directory in which you have installed nodeJS, run:
node server.js
Navigate to the URL
http://localhost:5000/test.html
This should work. Here is my directory configuration:
C:\NodeJSInstallLocation\angularjs
var connect = require('connect'),
serveStatic = require('serve-static');
connect().use(
serveStatic("angularjs")
).listen(5000);
You might want to try something like this
var express = require('express');
var app = express();
app.use(express.static('angularjs'));
Try this...
const express = require('express');
const app = express();
const path = require('path');
app.use('',express.static(path.join(__dirname, '/static/')));

Mustache and Express in Node

I'm trying desperately to tie in the mu2 module into Express in node.js. However, I can't seem to figure it out, and when trying to run the example using the mu2express module, I keep getting this error when trying to run:
module.js:340
throw err;
^
Error: Cannot find module 'mu2Express'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/myapp.js:1:80)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
Is this an error caused by something locally, or with the module itself? Right now, I'm using the node http module to work with mu2 exclusively, but I really want to use express is possible.
Can anyone out there help? Is there more information I should provide? I'm very new to node.js and I could use some direction if possible!
Look into consolidate:
https://github.com/visionmedia/consolidate.js/
It is made by the man behind Express himself and has support for Hogan and Handlebars.
EDIT:
You can initialize Express with this for consolidate
var express = require('express');
var cons = require('consolidate');
var app = express();
app.engine('html', cons.hogan);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
Renders will now be served from /views with HTML extension and Mustache-flavor syntax.
app.get('/', function (req, res) {
res.render('index', {msg: 'Hello world!'}
});
And a basic template, again just mustache-flavor syntax
Hello {{msg}}
You're requiring the mu2Express module from the myapp.js file, so you need to install it first.
You'll need to create a package.json file with at least the following content:
{
"name": "myProject",
"version": "0.0.1",
"dependencies": {
"mu2express": "~0.0.1"
}
}
And I'm not sure, that the examples are good for this project, you may need to require mu2express instead of mu2Express.

Resources