I am attempting to make a simple AngularJS application that will pull from a RESTful API to fill out some "cards" on the screen.
Here is my Angular $http request (xxx.xxx.xxx.xxx is to hide my public IP):
$http({method : "GET", url : "http://xxx.xxx.xxx.xxx:9000/api/cards"}).then(function mySucces(response) {$scope.listData = response.data;});
This request works for actual JSON formatted calls, such as a call to http://data.consumerfinance.gov/api/views.json - it works great here.
However, it does not work when calling my RESTful API created with node-restful using nodejs. I can view the API data in the web browser, but I cannot get it to pull in the angular app. I am about 99.99% sure it is because I am not returning it as JSON format but mongodb format from my API.
I am asking for assistance to find a way to have my RESTful API return JSON formatting.
Here is my server.js:
// Dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var parth = require('path');
// Connect to Mongoose (MongoDB)
mongoose.connect('mongodb://localhost/rest_test')
// Build Express
var app = express();
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());
// Router
app.use('/api', require('./routes/api'));
// Start Server
app.listen(9000)
console.log('Yo, stuffs on port 9000')
Here is my routes.js:
// Dependencies
var express = require('express')
var router = express.Router();
// Models
var Card = require('../models/cards')
// Routes
Card.methods(['get', 'put', 'post', 'delete']);
Card.register(router, '/cards')
// Return Router
module.exports = router;
And here is my mongoose schema (cards.js):
// Dependencies
var restful = require('node-restful');
var mongoose = restful.mongoose;
// Schema
var cardSchema = new mongoose.Schema({
title: String,
author: String,
desc: String,
cardtype: String
});
// Return model
module.exports = restful.model('Cards', cardSchema);
One of my issues is that I do not know where I need to but the function(res, resp, next) in, which file and what should it look like?
Thanks in advance.
I Wiresharked it. It is totally sending JSON back. This is an issue with the application for whichever reason.
Thank you.
In your server.js file simple put this code.
`var logger= function (req, res, next) {
console.log('LOGGED');
next();
}; app.use(logger);`
Related
I am trying to set up a post request on a node.js server such that if any data from the json report is missing it just throws an error rather than doing anything with the database.
My server is a mongodb server using express and body-parser.
Here is the code I want to create
app.post('/update', function(req, res) {
const params = req.body;
const newData = {
id: params.id,
data: params.data
.../a ton more data
};
if (anything is missing from newData (any field is undefined) ) {
res.send({err: true});
} else {
//Some cool database things
}
}
I realize that I could just check if any of my fields are undefined however that is not really elegant especially when I am about to have about 20 fields in the incoming data.
You can create a JSON schema for your request and then validate your actual request with the existing JSON schema using npm package like ajv
You can checkout more about JSON schema from here. You can create your own JSON schema from here.
there is a module in node JS called JOI which does the initial payload validation.
And for using this with express JS check express-joi
Express Joi Link
var express = require('express');
var expressJoi = require('express-joi');
var Joi = expressJoi.Joi; // The exposed Joi object used to create schemas and custom types
var app = express();
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use(errorHandler);
// Use the Joi object to create a few schemas for your routes.
var getUsersSchema = {
limit: expressJoi.Joi.types.Number().integer().min(1).max(25),
offset: expressJoi.Joi.types.Number().integer().min(0).max(25),
name: expressJoi.Joi.types.String().alphanum().min(2).max(25)
};
var updateUserSchema = {
userId: Joi.types.String().alphanum().min(10).max(20),
name: Joi.types.String().min(3).max(50)
};
// Attach the validator to the route definitions
app.get('/users', expressJoi.joiValidate(getUsersSchema), handleUsers);
app.put('/users/:userId', expressJoi.joiValidate(updateUserSchema),
handleUpdateUser);
app.listen(8080);
I'm not new to JavaScript but I am new to Node.js and back end languages. I have a very simple question.
I've installed and setup Node.js on my computer and I'm attempting to get a server going between my static files & directory(s) and my browser to be able to send and receive requests. I've downloaded Braintree's free Sandbox (found here) for practice to get some faux transactions going just to gain a better understanding of how this can work.
I set up a local server by running npm install -g http-server on my command line and then http-server to set it up.
I then received the following message in my command line:
Starting up http-server, serving ./public
Available on:
http://127.0.0.1:8080
http://10.0.1.4:8080
Hit CTRL-C to stop the server
So, with this setup...if I wanted to do get() and post() methods and see it rendered and communicating between my "server" and my static files. How do I do this? For example, if I were to set up Braintree's sandboxed environment and then create a clientToken using the following code from Braintree's website
const http = require('http'),
url = require('url'),
fs = require('fs'),
express = require('express'),
braintree = require('braintree');
const gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: "xxxxx",
publicKey: "xxxxx",
privateKey: "xxxxx" //blocked out real numbers for privacy
});
Here is the remaining code I hae to create a "client Token" for a transaction...and here is the guide I'm following via Braintree's website...
http.createServer((req,res) => {
gateway.clientToken.generate({
},(err, response) => {
if(err){
throw new Error(err);
}
if(response.success){
var clientToken = response.clientToken
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(clientToken);
res.end("<p>This is the end</p>");
} else {
res.writeHead(500, {'Content-Type': 'text/html'});
res.end('Whoops! Something went wrong.');
}
});
}).listen(8080,'127.0.0.1');
So, my question is...if I wanted to generate send a token to a client using the get() method...how would I do that? Would it have to be a separate js file? How would they be linked? If they're in the same directory will they just see each other?
Here is an example on Braintree's website of how a client token may be sent:
app.get("/client_token", function (req, res) {
gateway.clientToken.generate({}, function (err, response) {
res.send(response.clientToken);
});
});
How could this be integrated into my current code and actually work? I apologize if these are elementary questions, but I would like to gain a better understanding of this. Thanks a lot in advance!
I don't know much about braintree, but usually you would use somthing like express.js to handel stuff like this. So I'll give you some quick examples from an app I have.
#!/usr/bin/env node
var http = require('http');
var app = require('../server.js');
var models = require("../models");
models.sync(function () {
var server = http.createServer(app);
server.listen(4242, function(){
console.log(4242);
});
});
So that's the file that gets everything started. Don't worry about models, its just syncing the db.
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
// share public folder
app.use(express.static(path.join(__dirname, 'public')));
require('./router.js')(app);
module.exports = app;
next up is the server.js that ties eveything together. app.use() lines are for adding middleware and the app.use(logger('dev')); sets the route logger for what your looking for.
app.use(express.static(path.join(__dirname, 'public'))); shares out all files in the public directory and is what your looking for for static files
var path = require('path');
module.exports = function(app){
//catch
app.get('*', function(req, res){
res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
});
}
last piece is the router.js. This is were you would put all of you get and post routes. generally I've found that if you see app.get or app.post in examples there talking about express stuff. It's used a lot with node and just makes routing way easier.
Also if your using tokens a route would look like this.
app.get('/print', checkToken, function(req, res){
print.getPrinters(function(err, result){
response(err, result, req, res);
});
});
function checkToken(req, res, next){
models.Tokens.findOne({value: req.headers.token}, function(err, result){
if(err){
res.status(500).send(err);
}else if(result == null){
console.log(req.headers);
res.status(401).send('unauthorized');
}else{
next();
}
});
}
so any route you want to make sure had a token you would just pass that function into it. again models is for db
I have been looking for solutions for a couple days now trying to google it and all and now i am here. I am trying to setup subdomains for my app using express-subdomain package. However in the example below the app ALWAYS returns app.get route and skips the other subdomain routes specified.
I have also in turn added the hosts file url so i know that should not be the issue.
It must be in my code for some reason it always ends up displaying Detect Region route even when accessing oce.localhost:3000.
Please help me :)
Server.js
var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
// Region routes
var router = express.Router();
var na = require('./routes/region/na.js');
var oce = require('./routes/region/oce.js');
router.use(subdomain('na.localhost', na));
router.use(subdomain('oce.localhost', oce));
app.get('/', function(req, res) {
res.send('Detect Region and send to correct subdomain!');
});
app.listen(3000);
routes/region/oce.js
var
express = require('express'),
router = express.Router();
router.get('/', function(req, res) {
res.send('Your are in OCE Region!');
});
module.exports = router;
And na.js is pretty much the name as oce.js
Cheers
You are setting your subdomains in the router variable but you don't tell your app to use it.
You have to do that :
app.use(router);
You put it in place of your current app.get.
Edit
You could also put your app.get after the app.use(router) so that it will act as a default route. (When you are neither on oce or na, it will use it)
Edit after some testing
Alright I've been able to make it work using express-vhost. I just updated your server.js like so :
var subdomain = require('express-vhost');
var express = require('express');
var app = express();
// Region routes
var router = express.Router();
var na = require('./routes/region/na.js');
var oce = require('./routes/region/oce.js');
subdomain.register('na.localhost', na)
subdomain.register('oce.localhost', oce)
app.use(subdomain.vhost(app.enabled('trust proxy')));
app.get('/', function(req, res) {
res.send('Detect Region and send to correct subdomain!');
});
app.listen(3000);
I'm attempting to build a MEAN app and trying to test POSTing with POSTMAN. When I do, I keep getting the dreaded "TypeError: Cannot read property 'name' of undefined". If I type in a simple string, the POST goes through fine. But when I use "req.body.name" I get the error. I've looked in every place and I'm not seeing my mistake. I even followed the suggestions on this thread with no luck. Any help or suggestions would be greatly appreciated.
Here's the code I am currently working with in my server.js file:
const express = require('express');
var bodyParser = require('body-parser');
var Bear = require('./models/bear')
var path = require('path');
var mongoose = require('mongoose');
var router = express.Router();
var app = express();
var staticAssets = __dirname + '/public';
app.use(express.static(staticAssets));
app.use('/api', router)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Routes for my API
//===================================
// middleware to use for all requests
router.use(function(req,res,next){
// logging happens here
console.log('Something will happen.');
next(); // Head to the next router...don't stop here
});
// Test router to make sure everything is working (accessed at GET http://localhost:3000/api)
router.get('/', function(req, res){
res.json({message: 'hooray! welcome to our api!'})
})
//More routes will happen here with routes that end in "/bears"
router.route('/bears')
//Create a bear (accessed at POST http://localhost:3000/api/bears)
.post(function(req,res){
var bear = new Bear(); // Create a new instance of the bear model
console.log(req);
bear.name = req.body.name; // set the bears name (comes from the request)
//res.send(200, req.body);
bear.save(function(err){
if (err)
res.send(err);
res.json({message: 'Bear Created!!'});
});
});
//======================================
//var Products = require('./products.model.js');
var Product = require('./models/product.model');
var db = 'mongodb://localhost/27017';
mongoose.connect(db);
var server = app.listen(3000);
console.log("App is listening on port 3000");
Thanks.
Also, the url I'm trying to use inside of POSTMAN is http://localhost:3000/api/bears
Express processes requests Top-Down, meaning if you require a piece of functionality to be applied to all routes via middleware, than that middleware needs to be added to your app before any routes that require it. This is usually the case for middleware such as body-parser.
When using Router Middleware, you don't typically construct the router in the same file as the actual Express app that will use it as middleware. Instead, place it in a separate file and/or directory for organization purposes, this is considered a best practice.
Express Apps can be structured like so
/lib
/models
bear.js
product.js
/node_modules
/public
/css
/routes
api.js
package.json
server.js
The routes directory is where you would place any applicable Router Middleware files such as your api router. server.js is your main Express App and public is where your static assets are stored. lib is directory that contains any business logic files and models.
The actual Express app and Router files should look something like this
server.js
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const apiRouter = require('./routes/api');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, public)));
app.use(/api, apiRouter);
app.listen(port, () => {
console.log(`Listening on port ${port});
});
module.exports = app;
routes/api.js
'use strict';
const router = require('express').Router();
const Bear = require('./lib/models/bear');
router.use((req, res, next) => {
// logging happens here
console.log('Something will happen.');
next(); // Head to the next router...don't stop here
});
router.get('/', (req, res) => {
return res.json({ message: 'hooray! welcome to our api!'})
});
router.route('/bears')
//Create a bear (accessed at POST http://localhost:3000/api/bears)
.post((req, res) => {
var bear = new Bear(); // Create a new instance of the bear model
console.log(req);
bear.name = req.body.name; // set the bears name (comes from the request)
//res.send(200, req.body);
bear.save((err) => {
if (err)
return res.send(err);
return res.json({message: 'Bear Created!!'});
});
});
module.exports = router;
To note, you could break up your API even further to increase the amount of decoupling. An example of this would be to move the /api/bear route to its own router middleware and into its own route file. Then simply add it to your routes/api.js router as a middleware like you would in server.js. If your app is going to have a decent sized API, then this would be the best approach because it would allow the most flexibility when it comes to applying middleware to only certain routes and would make maintaining the source much easier.
Im a newbie on node.js, I just wanted to know how to extract the requested url in this code
app.use(express.static(__dirname+"/public"));
Thank you for your help
As you can see on the static middleware source, a middleware is basic a function that receives the parameters (request, response, next_function).
So you could create a function that reads the url before it goes to static middleware.
var express = require('express');
var app = express();
var staticfn = express.static(__dirname+'/public');
app.use(function (req,res,next) {
console.log(req.url);
var sendStream = staticfn(req,res,next);
console.log(sendStream.path);
});
app.listen(3000)
As you can see on the send package used by the static middleware, the send function returns a object called SendStream.