0
You’re making this mistake after npm run dev
(I’ve installed the Mongo and the npm models you need)
npm ERR! Missing script: dev
npm ERR! A complete log of this run can be found in: npm ERR! C: Users Getin-sds Appdata Roaming npm-cache_logs 2018-11-29T19_11_17_439Z-debug.log
Follow the server.js code
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const ObjectId = require('mongodb').ObjectID
const MongoClient = require('mongodb').MongoClient
const uri = "mongodb://Carlos-Antonio:[email protected]:37735/meubanco"
app.use(bodyParser.urlencoded({ extended: true }))
MongoClient.connect(uri, (err, client) => {
if (err) return console.log(err)
db = client.db('meubanco')
app.listen(3000, () => {
console.log('Servidor está rodando na porta 3000.')
})
})
app.set('view engine', 'ejs')
app.route('/')
.get(function (req, res) {
const cursor = db.collection('data').find()
res.render('index.ejs')
})
.post((req, res) => {
db.collection('data').save(req.body, (err, result) => {
if (err) return console.log(err)
console.log('Dados gravados com sucesso.')
res.redirect('/show')
})
})
app.route('/show')
.get((req, res) => {
db.collection('data').find().toArray((err, results) => {
if (err) return console.log(err)
res.render('show.ejs', { data: results })
})
})
app.route('/edit/:id')
.get((req, res) => {
var id = req.params.id
db.collection('data').find(ObjectId(id)).toArray((err, result) => {
if (err) return res.send(err)
res.render('edit.ejs', { data: result })
})
})
.post((req, res) => {
var id = req.params.id
var nome = req.body.nome
var sobrenome = req.body.sobrenome
db.collection('data').updateOne({ _id: ObjectId(id) }, {
$set: {
nome: nome,
sobrenome: sobrenome
}
}, (err, result) => {
if (err) return res.send(err)
res.redirect('/show')
console.log('Dados atualizados com sucesso.')
})
})
app.route('/delete/:id')
.get((req, res) => {
var id = req.params.id
db.collection('data').deleteOne({ _id: ObjectId(id) }, (err, result) => {
if (err) return res.send(500, err)
console.log('Dado excluído com sucesso.')
res.redirect('/show')
})
})
this here the file where I put
{
"name": "projeto",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"ejs": "^2.6.1",
"express": "^4.16.4",
"mongodb": "^3.1.10"
},
"devDependencies": {
"nodemon": "^1.18.7"
}
}
Good afternoon, is absent from the file
package.json
which is at the root of your project the scriptdev
running the fileserve.js
. Between there and just add"dev": "node serve.js"
and save thenpm run dev
should work, but that’s if serve.js is in the same folder as package.json if it’s not needed by the path to serve.js.– Chance