I am new to express and currently having issues connecting to an api
below is my code
const express = require("express");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const flash = require("connect-flash");
const fetch = require("node-fetch");
const axios = require("axios");
const app = express();
const dotenv = require("dotenv");
dotenv.config();
let sessionOptions = session({
secret: "Javascript is so cool",
store: MongoStore.create({ mongoUrl: process.env.CONNECTIONSTRING }),
resave: false,
saveUninitialized: false,
cookie: { maxAge: 1000 * 60 * 60 * 24, httpOnly: true },
});
app.use(sessionOptions);
app.use(flash());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static("public"));
app.set("views", "views");
app.set("view engine", "ejs");
const router = require("./router");
const apiRouter = require("./apiRouter");
const options = {
url: "https://rms.pythonanywhere.com/api",
method: "",
json: {},
};
app.use("/", router);
app.use(options.url, apiRouter);
module.exports = app;
Below is the file i created for routes that goes to the api
const express = require("express");
const apiRouter = express.Router();
const request = require("request");
const fetch = require("node-fetch");
const userController = require("./controllers/userController");
const pageController = require("./controllers/pageController");
apiRouter.post("/user/create", async function (req, res) {
const api_url = "https://rms.pythonanywhere.com/api";
const fetch_res = await fetch(api_url, options);
const json = await fetch_res.json();
res.json(json);
});
module.exports = apiRouter;
When i submit my registration form i want it to send it post request to the url of the api and recieve its response. But whenever i send a post request to the api url it returns an error of (can't send post request to /user/create)
What i really want to achieve is to connect to this api link (rms.pythonanywhere.com/api) and be able to direct my routes for registration and login with some other routes to the api. I've been battling with this for over a week now. Please guys help me out, I'm new to express
Thanks in advance guys
I think your express app is unable to find the apiRouter so, you need to use express router first with the help of express.use() method.
You can add below line to your code.
express.use("/", apiRouter);
Thanks guys for your response. Although none was actually the solution, but it gave me a clue of what i needed to do.
What i used in solving my issues was axios. i required("axios") after installing from npm store and den i used it in my apiRouter.post("/user/create") route. Below is what i did to solve the issue
apiRouter.post("/register", async function (req, res) {
const api_url = "https://rms.pythonanywhere.com/api";
axios.post(api_url + "/user/create/", {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
password: req.body.password,
user_type: req.body.user_type,
username: req.body.username,
}). then((response) =>{}).catch((error){})
});
I was able to send the data's from my post request made in apiRouter.post(/register) and recieved the success response in my then() and error response in my catch() block.
Related
I use vue3, vuex, express.js and mysql. In the below router get method, I call "console.log(req.body)" and shows "[object Object]", and I call "console.log(req.body.userid)" and shows "undefined".
router.get('/',async function(req,res){
const userId = req.body.userid;
console.log("req body is: "+req.body);
console.log("req.body.userid is: "+req.body.userid);
.....
}
In the below method, I pass userid value as a json object. I call "console.log("post userid: "+userinfo.userid);" and shows the the right value "1";
async getsp(){
var userinfo = JSON.parse(localStorage.getItem('user'));
console.log("post userid: "+userinfo.userid);
var userid = userinfo.userid;
var obj = {userid};
return await axios.get('//localhost:8081/getSp',obj)
.then(...)
},
And in the main router file I used body-parser, the file context is below:
require("dotenv").config();
const express = require('express');
const bodyParser = require('body-parser');
var cors = require('cors');
const signup = require('./userSignUp');
const login = require('./userLogin');
const createEvsp = require('./createEvsp');
const getSp = require('./getSp');
//const createFile = require('./createFile');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
app.use(cors())
app.use(express.json());
app.use(
express.urlencoded({
extended: true
})
);
app.use("/signup",signup);
app.use("/dologin",login);
app.use("/createEvsp",createEvsp);
app.use("/getSp",getSp);
//app.use("/createFile",createFile);
app.listen(8081,function () {
console.log('Server running at 8081 port');
});
The problem was an HTTP method understanding and how express works
To solve it it was needed to use the express middleware /:userid for accessing to the parameter using req.params.userid
According to the http standards for sending the data we generally use POST request.
There is a good answer in stack here Information about Get HTTP Request
Sayf-Eddine
I have this GET request that fetches data from a third party api. I want to check if there is new data every 5-10 minutes or so. Right now i have this setup on my backend.
exports.get_alerts = async (req, res) => {
const alertsUrl = `https://www.g2smart.com/g2smart/api/alert?cpo=${req.params.cpo}&status=Opened&limit=10&page=1`;
const axios = require("axios");
const auth = await refreshToken;
const currTime = moment().subtract(1, "days").format("X");
const newAlertsData = [];
const availableUsers = await axios.get(
"http://localhost:5000/api/schedule/available"
) ....
and on the front end i have this code to send a get request to my alerts api endpoint.
getAlerts = async () => {
axios
.get("/api/alerts/total_fr_hpc")
.then((response) => console.log(response.data))
.catch((err) => console.log(err));
};
timer = (time) => {
const date = new Date(time);
return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
};
componentDidMount() {
this.alertsInterval = setInterval(this.getAlerts, 900000);
}
componentWillUnmount() {
clearInterval(this.alertsInterval);
My question is can i have all this done on the backend only? I read a bit about websockets but that seems to be only for a continuous 2way connection between the backend and frontend.
I'd like to have something like that towards my third party apis on the node/express server, either fetching data at a set interval or a continuous connection checking for new data without having to make GET requests from my frontend. I want to be able to get new Data and store it into MongoDB even when there is nobody logged in to the client side.
I want that the data the users get is always up to date without having at least one person logged in to trigger the GET requests.
This is how my node/express server is currently setup
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const cors = require("cors");
const path = require("path");
const indexRouter = require("./routes/index");
const apiRouter = require("./routes/api"); //Import routes for "api" area of site
const app = express();
// CORS Middleware
app.use(cors());
app.options("*", cors());
// Bodyparser middleware
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(bodyParser.json());
// DB Config
const db = require("./config/keys").mongoURI;
// Connect to MongoDB
mongoose
.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => console.log("MongoDB successfully connected"))
.catch((err) => console.log(err));
// Passport middleware
app.use(passport.initialize());
// Passport config
require("./config/passport")(passport);
// Routes
// Add api routes to middleware chain.
app.use("/", indexRouter);
app.use("/api", apiRouter);
// Serve static assets (build folder) if in production
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const port = process.env.PORT || 5000;
app.listen(port, "0.0.0.0", () =>
console.log(`Server up and running on port ${port} !`)
);
I am not familiar with the axios as I always use the native fetch API. Maybe the isomorphic fetch can help.
I am writing a nodejs backend application for an application I developed with Vue. I need to create sessions for users' db operations and select databases according to the user. I wanted to use express-session for this, but the sessions I created are seen as undefined in different requests. How can I overcome this problem? I use history mode on Vue so my requests must go through the router for now. Also I can convey that I am open to alternative suggestions.
const express = require('express')
const app = express()
const productRequest = require("./controllers/products/products")
const customerRequest = require("./controllers/customers/customers")
const orderRequest = require("./controllers/orders/orders")
const controllerRequest = require("./controllers/controllers")
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
var session = require('express-session')
const cors = require("cors")
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}))
app.use(express.json());
app.use(cors());
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/products', productRequest)
app.use('/orders', orderRequest)
app.use('/customers', customerRequest)
app.use('/controllers', controllerRequest)
module.exports = app;
const express = require('express');
const controllers = express.Router();
const login = require('./login.js')
const userkey = require("./userkey");
controllers.post("/login", (req, res, next) => {
login(req.body, response => {
if (response[0].total == 0) {
res.status(204).json({
message: "Fail",
result: null
})
} else if (response[0].total == 1) {
/* SESSION SETTED HERE */
req.session.corp = response[0].corp
console.log(req.session.corp)
/* HERE SEEMS CREATED */
res.status(200).json({
message: "Connected",
result: response
})
}
})
});
controllers.post("/userkey", (req, res, next) => {
/* CANT USE HERE response UNDEFINED*/
console.log(req.session.corp)
userkey([req.body, req.session.corp], response => {
res.status(200).json({
data: response
})
})
});
module.exports = controllers;
I had some massive headaches with sessions and would have kept going until I read
Also I can convey that I am open to alternative suggestions.
Please then consider using JWT
as simple as this :
var jwt = require('jsonwebtoken');
var token = jwt.sign({ foo: 'bar' }, 'shhhhh');
The jsonwebtoken package is simple to use and should match your needs.
I'm trying to set a cookie with a post method in order to do some db query and put it back in the cookie value, as well as returning a json with the user data.
It works, the cookie is set and I get the json on http://localhost:8080
but I get a message from the compiler:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
How can I fix it so it won’t make this error?
my file structure is:
root/ app.js
root/controllers/ cookie.controller.js
root/routes/ cookie.route.js
app.js
const express = require('express');
const cors = require('cors');
const cookieParser = require('cookie-parser');
const app = express();
const port = process.env.PORT || process.argv[2] || 8080;
app.use(cookieParser());
app.use(require('./routes/cookies'));
app.use(cors());
app.listen(port, () => console.log('cookie-parser demo is up on port: ' + port));
cookie.route.js
const express = require('express');
const cookieController = require('../controllers/cookies');
const router = express.Router();
router.use(require('cookie-parser')());
router.post('/', router.use(cookieController.getCookie));
module.exports = router;
cookie.controller.js
exports.getCookie = (req, res, next) => {
let auth = req.cookies.auth;
//...db queries, get userData
let userData = {
id: '123',
token: 'sfsdfs34',
email: 'user#gmail.com'
};
// if cookie doesn't exist, create it
if (!auth) {
res.status(200)
.cookie('auth', userData.id)
.json({ message: 'it works!', user: userData });
req.cookies.auth = userData.id;
}
next();
};
You're modifying the request cookie headers after sending the response at the end of your getCookie controller. You should remove req.cookies.auth = userData.id, and use res.cookie() instead before sending the response.
const express = require('express')
const cookieParser = require('cookie-parser')
const app = express()
app.use(cookieParser())
app.get('/', (req, res) => {
if (!req.cookies.auth) {
res.cookie('auth', { id: '123' })
}
res.json({ message: 'It worked!' })
})
app.listen(8080, () => console.log('http://localhost:8080))
Problem was solved after deleting the cors from app.js
I'm doing an application with Node.js, Express, MongoDB (mongoose), I'm trying to make the database connection in a separate file from server.js, but I'm having a hard time with connect-mongo.
First in my server.js I had this:
/* jshint esversion: 6 */
'use strict';
let express = require('express');
const db = require('./app/config/db');
const routes = require('./app/routes/routes');
const users = require('./app/routes/users');
let app = express();
const conn = db.connect();
app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine', 'hbs');
...
app.use('/', routes);
app.use('/users', users);
app.listen(3000);
module.exports = app;
This only handle the application routes, and the application server, then I had the next folder structure for my project:
myApp
|___app
|___bin
|___config
|___credentials.js
|___db.js
|___controllers
|___routes
|___views
|___node_modules
|___package.json
|___server.js
Welll insidde config folder I had two javascripts that handle the connection to the database, in the credentials.js literally only had the credentials for the access of the database.
Then my problem is inside the db.js, next I show you the file:
/* jshint esversion: 6 */
'use strict';
let mongoose = require('mongoose'),
async = require('async'),
express = require('express');
const credentials = require('./credentials');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
let db = mongoose.connection,
app = express();
exports.connect = function(done){
const connection = mongoose.connect(credentials.host, credentials.database, credentials.port, credentials.db);
db.on('error', (error =>{
console.log("Error estableciendo la conexion");
process.exit(1);
}));
db.on('open', (argv)=>{
db.db.listCollections().toArray((err, collections)=>{
collections.forEach(x => console.log(x));
});
});
/* Define sessions in MongoDB */
app.use(session({
secret: credentials.sessionSecret,
store: new MongoStore({ dbPromise: db })
}));
}
I got the next error:
Error with nodemon server.js
Do you know how to initiate connect-mongo using this project structure?
By the way, in the credentials.js file I setup Bluebird as my promise library.
In advance thank you!
The problem was, like #MananVaghasiya said, that my variable db was not a Promise, this is a bug inside the mongoose project, so I changed the connection type to a basic uri connection with mongoose and then after the query of login I set the session.
The code it's looking like this at this time, so thank you for your time.
module.exports.login = (req, res)=>{
const mail = req.body.mail.replace(/^\s\s*/, '').replace(/\s\s*$/, ''),
password = req.body.password;
user.findOne({$and:[{'mail' : mail}, {'password': password}]}, (err, user)=>{
if(err){
res.send(err);
} else {
/* Define sessions in MongoDB */
app.use(session({
secret: credentials.sessionSecret,
store: new MongoStore({ mongooseConnection: db }),
saveUnitialized: true,
resave: false,
cookie:{
path: "/"
},
name: user.role
}));
return user;
}
});
};