I have run this command on my root console
npm install --save
At the root of my folder
This is a snippet from my ionic-gcm.js
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
var server = app.listen(3000, function(){
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});
When I run this command, node ionic-gcm.js
I keep getting this error
Error: Cannot find module body-parser
===============================package.json======================
{
"name": "ionic-gcm",
"version": "1.0.0",
"description": "ionic-gcm: An Ionic project",
"dependencies": {
"express": "^4.13.3",
"gulp": "^3.5.6",
"gulp-concat": "^2.2.0",
"gulp-minify-css": "^0.3.0",
"gulp-rename": "^1.2.0",
"gulp-sass": "^1.3.3",
"node-gcm": "^0.11.0",
"server": "0.0.3"
},
"devDependencies": {
"bower": "^1.3.3",
"gulp-util": "^2.2.14",
"shelljs": "^0.3.0"
},
"cordovaPlugins": [
"org.apache.cordova.device",
"org.apache.cordova.console",
"com.ionic.keyboard",
"cordova-plugin-whitelist"
],
"cordovaPlatforms": [
"android"
]
}
you are missing body-parser. just type
npm install --save body-parser
Related
Hi i am having trouble when trying to deploy my mern stack to heroku. every way i have tried to deploy it i get the same error of get the error Cannot GET /. i have gone through the tutorials but still no luck
this is my server. js file
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const users = require("./routes/api/users");
const payees = require("./routes/api/payees");
const transactions = require("./routes/api/transactions");
const path = require('path')
const app = express();
// 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 }
)
.then(() => console.log("MongoDB successfully connected"))
.catch(err => console.log(err));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.json());
// Passport middleware
app.use(passport.initialize());
// Passport config
require("./config/passport")(passport);
// Routes
app.use("/api/users", users);
app.use("/api/payees", payees);
app.use("/api/transactions", transactions)
if(process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'client', 'build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server up and running on port ${port} !`));
module.exports = { app };
and this is my package.json file
{
"name": "STUBANK",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"test": "mocha",
"heroku-postbuild": "cd client && npm install && npm run build"
},
"author": "Ojini Jude",
"license": "MIT",
"dependencies": {
"#material-ui/core": "^4.11.2",
"axios": "^0.21.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"classnames": "^2.2.6",
"crypto-random-string": "^3.3.0",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"is-empty": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"jwt-decode": "^3.1.2",
"mdbreact": "^5.0.1",
"mocha": "^8.2.1",
"mongoose": "^5.3.6",
"particles-bg": "^2.5.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"password-validator": "^5.1.1",
"react": "^17.0.1",
"react-bootstrap": "^1.4.0",
"react-dom": "^17.0.1",
"react-dropdown": "^1.9.0",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.1",
"react-search-box": "^2.0.2",
"react-search-field": "^1.2.0",
"react-select": "^3.1.1",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"supertest": "^6.0.1",
"validator": "^13.5.1"
},
"devDependencies": {
"concurrently": "^4.1.2",
"nodemon": "^1.19.4"
},
"engines": {
"node": "12.16.0"
}
}
Any help would be greatly appreciated.
file structure
I'm trying to deploy my first MERN website to heroku. I have been following different tutorials but I am having trouble connecting the front end with the back end, although they connect fine in development mode.
Just to clarify: the only one that is bein deployed is the server side. I am not being able to deploy the front end.
client's package.json proxy:
"proxy": "http://localhost:5000"
Server package.json:
{
"name": "pictshare",
"version": "1.0.0",
"description": "Image Sharing Application",
"main": "server.js",
"scripts": {
"start": "node server.js",
"server": "nodemon server",
"build": "cd client && npm run build",
"install-client":"cd client && npm install",
"heroku-postbuild":"npm run install-client && npm run build",
"client": "npm start --prefix client",
"dev": "concurrently -n 'server,client' -c 'red, green' \"npm run server\" \"npm run client\""
},
"author": "",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"config": "^3.3.1",
"ejs": "^3.0.2",
"express": "^4.17.1",
"express-validator": "^6.4.0",
"form-data": "^3.0.0",
"gravatar": "^1.8.0",
"gridfs-stream": "^1.1.1",
"jsonwebtoken": "^8.5.1",
"merge-images": "^1.2.0",
"method-override": "^3.0.0",
"moment": "^2.24.0",
"mongoose": "^5.9.7",
"multer": "^1.4.2",
"multer-gridfs-storage": "^4.0.2",
"node": "^13.12.0",
"nodemailer": "^6.4.6",
"path": "^0.12.7",
"react-draggable": "^4.2.0",
"request": "^2.88.2"
},
"devDependencies": {
"concurrently": "^5.1.0",
"minimist": "^1.2.5",
"nodemon": "^2.0.2"
}
}
Server.js:
const express = require('express');
const connectDB = require('./config/db');
const app = express();
const bodyParser = require('body-parser')
connectDB();
//Initialize Middleware
app.use(express.json({ extended: false }));
app.use(bodyParser.urlencoded({ extended: false }))
app.get('/', (req, res) => res.send('API Running'));
...
app.use('/api/posts', require('./routes/api/posts'));
if (process.env.NODE_ENV === 'production'){
app.use(express.static('client/build'))
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
Mongoose:
const mongoose = require('mongoose');
const config = require('config');
const db = process.env.MONGODB_URI || config.get('mongoURI');
const connectDB = async () => {
try {
await mongoose.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
useNewUrlParser: true,
});
console.log('MongoDB Connected...');
} catch(err) {
console.error(err.message);
process.exit(1);
}
}
module.exports = connectDB;
I really appreciate any ideas or points of views!!
Thank you so much everyone!
Maybe you already did the job, let me point out a thing here in your code:
app.use(express.static('client/build'))
you maybe need to put like this :
const app = express();
const path = require('path');
app.use('/static', express.static(path.join(`${__dirname}/client/build`)));
then just send the file when the server is online:
app.get('/*', (req, res) => {
res.sendFile(path.join(`${__dirname}/client/build/`));
});
If you have .env file. Try adding this NODE_ENV = production in it.
I'm trying to send a small demo made with React to production. I did not make the demo with create-react-app, I did it by hand. I used express.js to serve the application.
When I run the command "SET NODE_ENV = production node server.js" (I'm in Windows, that's why SET) and I go to the localhost I keep getting GET / 404, even though I already made the command "npm run build".
Since it's the first time I do this, I really have no clue what happens.
This is my package.json:
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --colors",
"serve": "webpack-dev-server --content-base build/ --color --
progress",
"start": "npm run build && npm run serve",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Pepe",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.4.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"css-loader": "^0.28.9",
"eslint": "3.15.0",
"eslint-config-airbnb": "14.1.0",
"eslint-loader": "^1.9.0",
"eslint-plugin-import": "2.2.0",
"eslint-plugin-jsx-a11y": "4.0.0",
"eslint-plugin-react": "6.10.0",
"extract-text-webpack-plugin": "^3.0.2",
"html-webpack-plugin": "^2.30.1",
"identity-obj-proxy": "^3.0.0",
"jshint": "^2.9.6",
"style-loader": "^0.20.2",
"url-loader": "^0.5.8",
"webpack": "^3.3.0",
"webpack-dev-server": "^2.9.7"
},
"dependencies": {
"bootstrap": "^4.0.0",
"compression": "^1.7.3",
"express": "^4.16.4",
"mime": "^1.4.1",
"morgan": "^1.9.1",
"normalize.css": "^8.0.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router-dom": "^4.1.1",
"uuid": "^3.2.1"
},
"standard": {
"ignore": [
"/build/"
]
},
"eslintConfig": {
"extends": [
"standard",
"standard-jsx"
]
},
"stylelint": {
"extends": "stylelint-config-standard"
},
"optionalDependencies": {
"win-node-env": "^0.4.0"
}
}
And here is my server.js:
const { createServer } = require('http');
const express = require('express');
const compression = require('compression');
const morgan = require('morgan');
const path = require('path');
const normalizePortprocess = port => parseInt(port, 10);
const PORT = normalizePortprocess(process.env.PORT || 3000);
const app = express();
const dev = app.get('env') !== 'production';
if (!dev) {
app.disable('x-powered-by');
app.use(compression());
app.use(morgan('common'));
app.use(express.static(path.resolve(__dirname, 'build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
}
if (dev) {
app.use(morgan('dev'));
}
const server = createServer(app);
server.listen(PORT, err => {
if(err) throw new err;
console.log(`Server listen in ${PORT}`);
});
Everything would seem to work correctly except that despite having executed to many times the command "npm run build", still nothing appears
You are running in a development environment, and only handling that GET request if !dev.
if (!dev) { // here is the problem
// ...
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
}
Instead, either remove the if statement and always handle this route, or change it to if(dev).
I have a nodejs app. The app runs with pm2. When I execute "pm2 start app.js". pm2 is started. But when I do curl http://localhost:3000 , it says : Failed to connect to localhost port 3000: Connection refused. But port 3000 is open. I am confused, Please help me out.
My Dockerfile
FROM keymetrics/pm2:latest-alpine
# Create app directory
RUN mkdir -p /usr/cl/app
WORKDIR /usr/cl/app
# Install app dependencies
COPY package*.json ./
COPY . .
RUN npm install
RUN npm install pm2 -g
EXPOSE 3000
CMD [ "npm", "start" ]
My Package.json
{
"name": "cs",
"version": "1.0.0",
"description": "Cs",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "pm2 start app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"async": "^2.6.0",
"axios": "^0.18.0",
"base64-encode-decode": "0.0.3",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"csv": "^1.2.1",
"csv2sql-lite": "0.0.6",
"dotenv": "^4.0.0",
"epipebomb": "^1.0.0",
"express": "^4.16.3",
"fast-csv": "^2.4.1",
"getmac": "^1.4.1",
"ini": "^1.3.5",
"js-md5": "^0.7.3",
"multer": "^1.3.0",
"node-cache": "^4.2.0",
"node-xlsx": "^0.12.0",
"nodemailer": "^4.6.4",
"os": "^0.1.1",
"pg": "^6.4.2",
"serve-static": "^1.13.2",
"trim": "0.0.1"
}
}
Docker image and container is created. But when I do curl http://localhost:3000
Error says "Failed to connect to localhost port 3000: Connection refused. But port 3000 is open. I am confused, Please help me out.
app.js file is
"var express = require('express');
var bodyparser = require('body-parser');
var cors = require('cors');
var path = require('path');
require('dotenv').config({path: 'inireader.env'});
var app = express();
const port = 3000;
const routepostgres = require('./routes/routepostgres');
app.use(cors());
app.use(bodyparser.urlencoded({ extended: false }));
app.use(bodyparser.json());
app.use(express.static(path.join(__dirname,'public')))
app.use('/api', routepostgres);
app.listen(port, function(req, res){
console.log('Server started at port:'+ port);
});
app.get('/', function(req, res){
res.send('Welcome # browser');
console.log('Welcome # Console');
});
I'm making a nodejs + expressjs server, and a react fat client (SPA). After I install this package.json:
{
"name": "express-react",
"version": "1.0.0",
"description": "Some basic ReactJS",
"author": "Greg",
"license": "ISC",
"private": true,
"keywords": [
"reactjs",
"webpack"
],
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.15.5",
"morgan": "~1.9.0",
"pug": "^2.0.0-rc.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"serve-favicon": "~2.4.5"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-react-html-attrs": "^2.1.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"webpack": "^3.10.0",
"webpack-dev-middleware": "^2.0.5"
}
}
I got this error when I run DEBUG=express-react:* & npm start command:
Error: Cannot find module 'webpack-dev-middleware'
My app.js was generated by the express-generator:
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 config = require('./webpack.config.js');
const webpackmiddleware = require('webpack-dev-middleware');
var webpack = require('webpack');
const compiler = webpack(config);
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(webpackmiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath,
stats: {colors: true},
}));
.....
It confuses me.. I defined the webpack-dev-middleware package in the package.json file, but it said its not found.. why?
Have anybody met this problem before?
Thanks for the responses / advices in advance!
webpack-dev-middleware is mentioned in your devDependencies, not dependencies.
If your NODE_ENV is set to production, npm install only installs production dependencies and excludes development dependencies:
To install devDependencies, run:
npm install --dev