I am new to Node.JS I am trying to compile a node with express API but without success, Tried to debug the App just stops in the first module import, I create similar app from tutorial ran well but not saving input data, code bellow:
URL: localhost:3000/api/v1/students
Server.js
// Dependences
var bodyParser = require("body-parser");
var express = require("express");
var mongoose = require("mongoose");
var app = express();
//connect to database
mongoose.connect("mongodb://localhost/rest_test");
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.get("/api/v1",require("./routes/api"));
app.listen(3000,
function(req,resp)
{
console.log("is Working bitch!");
});
./routes/api.js
var express = require("express");
var router = express.Router();
var Students = require("../models/Students");
Students.methods(["get","post","put","delete"]);
Students.register(router, "/Students");
module.exports = router;
./models/Students.js
var restful = require("node-restful");
var mongoose= restful.mongoose;
var StudentSchema = new mongoose.Schema(
{
name : String,
course : String
});
module.exports = restful.model("Students",StudentSchema);
Solved,
Instead of get must be use:
app.use("/api/v1",require("./routes/api"));
Related
I am new in node js and want to build an application in which they are multiple APIs and all are settled in routes folder but here is the problem when I tried to call the API in index.js(main file) its only take one route either userAPI or cleanerAPI.
As both APIs have different URL.
var mongoose = require ('mongoose');
var express = require('express');
const bodyParser = require('body-parser');
var app = express();
const routerUser= require('./routes/userAPI')
mongoose.connect('mongodb://localhost:27017/Covid');
app.use(bodyParser.json());
app.use('/',router);
app.listen(4000,function(){
console.log("Server Running on 4000")
});
you can try this one also.
Main file(index.js or server.js)
const appRoutes = require('./Routes')
appRoutes(app)
Routes(index.js)
module.exports = app => {
app.use("/user", require('./users'));
app.use("/cleaner", require('./cleaner')); };
Routes(users.js)
const express = require('express');
const router = express.Router();
router.route('/list')
.post(function(req,res){......});
module.exports = router;
i want to show success flash message in express js ?
But i am getting following error
"Error: express-session is required. npm i express-session",(i installed express-session but still showing error )Where i am wrong ?
Here is my code
var express = require("express");
var app = express();
var path = require("path");
var bodyParser = require('body-parser');
var Router = require('router');
var api = express.Router();
var mysql = require('mysql');
var session = require('express-session');
const flash = require('express-flash-notification');
app.post('/addcontact', function(req, res){
var name =req.body.name;
var message =req.body.email;
var phone =req.body.phone;
var sql= "insert into contactus(name,message) values ('"+name+"', '"+message+"')";
con.query(sql,function(err,rows){
if(err) throw err;
})
req.flash('success', 'This is a flash message using the express-flash module.');
res.render('pages/contact');
});
I am trying to create a simple API using NodeJS.
I plan to separate the main api.js, route-definitions.js, route-logic.js in their own separate folders for them to be more structured and organized.
However, when I call app.route() in my route-definitions.js, it fails at compilation and saying that app.route is not a function.
server.js
var express = require('express')
var api = require("./api/api.js");
app = express();
port = process.env.PORT || 3000;
app.use('/', api);
app.listen(port);
api.js
var express = require('express');
var router = express.Router();
router.use('/read', require('./routes/route-definitions'));
module.exports = router;
route-definitions.js
'use strict';
module.exports = function(app) {
var operations = require('../controllers/route-logic.js')
//Route to check if a file with the same file name already exists
app.route('/getItems')
.post(operations.getItems);
}
When I try to run the API locally, and call /read/getItems, I get the error:
TypeError: app.route is not a function
What am I missing? I'm fairly new to Node and Express, but I know I'm not passing the app instance correctly or it is not set globally.
You can solve it by creating a child router in route-definitions.js
var router = require('express').Router();
var operations = require('../controllers/route-logic.js');
router.post('/getItems', operations.getItems);
module.exports = router;
In api.js you need to pass the router to the function you are exporting in route-definitions.js
var express = require('express');
var router = express.Router();
router.use('/read', require('./routes/route-definitions')(router));
module.exports = router;
You've used app in the route-definitions.js but looks like you forgot to pass it to that module.
Please have a look at below code:
var express = require('express');
var router = express.Router();
var app = express();
router.use('/read', require('./routes/route-definitions')(app));
module.exports = router;
I am trying to connect socket.io to my routing system, but despite the multiple methods I have read about to achieve this, I'm not sure the best approach with my setup as I have three degrees of separation from my server file and the route file that requires the socket.io configuration. Should I pass it as a constructor argument?
I tried passing the argument //Routing app.use(controllers)(io);, but ran into an error with the router module, so I don't believe I'm setting it correctly.
Error: var search = 1 + req.url.indexOf('?'); TypeError: Cannot read property 'indexOf' of undefined
Here is my setup.
app.js:
var express = require('express');
var app = express();
//Port Setting
var port = process.env.PORT || 3000;
//Server Port
var server = app.listen(port);
//External Packages
var io = require('socket.io')(server);
var redisAdapter = require('socket.io-redis');
var controllers = require('./app/controllers/routes');
//Routing
app.use(controllers);
routes.js:
var express = require('express');
var router = express.Router();
router.use('/app', require('./app-routes'));
router.use('/api', require('./api-routes'));
module.exports = router;
app-routes.js (where I would like to configure socket.io):
var express = require('express');
var app = express();
var appRoutes = express.Router();
/*==== / ====*/
appRoutes.route('/')
.get(activityFeed.load)
.post(activityFeed.filter);
Node JS require for the bittrex api... exactly where do it.
npm everything is installed including require...
var bittrex = require('bittrex-api');
on any page gives require is not defined
1) I'm using ui-router, server.js is not needed... I added one anyway.
var express = require('express');
var path = require('path');
//var logger = require('morgan');
//var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//var bcrypt = require('bcryptjs');
var app = express();
app.set('port', process.env.PORT || 8080);
//app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
//app.use(cookieParser());
app.use(express.static(path.join(__dirname, '/')));
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
now I want to add the bittrex ticker on the dashboard... so on the dashboard view I add
var market = 'BTC-LTC';
var bittrex = require('bittrex-api');
I turn on the server.js file and I get the require undefined message, I turn it off same message.
What am I doing wrong?