angularjs - 404 Error with simple Express routing -
i trying set routing in node.js app using express.js , mongoose.js using tutorial - https://thinkster.io/mean-stack-tutorial/ when test curl, gives me message:
(the curl script is: curl --data 'title=test&link=http://test.com' http://localhost:3000/#/posts
)
error: not found @ /users/adamz/flapper-news3/app.js:30:13 @ layer.handle [as handle_request] (/users/adamz/flapper-news3/node_modules/express/lib/router/layer.js:82:5) @ trim_prefix (/users/adamz/flapper-news3/node_modules/express/lib/router/index.js:302:13) @ /users/adamz/flapper-news3/node_modules/express/lib/router/index.js:270:7 @ function.proto.process_params (/users/adamz/flapper-news3/node_modules/express/lib/router/index.js:321:12) @ next (/users/adamz/flapper-news3/node_modules/express/lib/router/index.js:261:10) @ /users/adamz/flapper-news3/node_modules/express/lib/router/index.js:603:15 @ next (/users/adamz/flapper-news3/node_modules/express/lib/router/index.js:246:14) @ function.proto.handle (/users/adamz/flapper-news3/node_modules/express/lib/router/index.js:166:3) @ router (/users/adamz/flapper-news3/node_modules/express/lib/router/index.
for reference app.js file follows:
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 routes = require('./routes/index'); var users = require('./routes/users'); var mongoose = require('mongoose'); require('./models/posts'); require('./models/comments'); mongoose.connect('mongodb://localhost/news'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); // uncomment after placing favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false })); app.use(cookieparser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); // catch 404 , forward error handler app.use(function(req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); // error handlers // development error handler // print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); module.exports = app;
and routes/index.js is:
var express = require('express'); var router = express.router(); var mongoose = require('mongoose'); var post = mongoose.model('post'); var comment = mongoose.model('comment'); /* home page. */ router.get('/', function(req, res, next) { res.render('index', { title: 'express' }); }); router.get('/posts', function(req, res, next) { post.find(function(err, posts){ if(err){ return next(err); } res.json(posts); }); }); router.post('/posts', function(req, res, next) { var post = new post(req.body); post.save(function(err, post){ if(err){ return next(err); } res.json(post); }); }); module.exports = router;
is out of order? copied , pasted directly tutorial, node.js running, mongodb running, i'm not sure what's going on. let me know if other info help. in advance help.
i believe curl script should include quotations around site:
curl --data "title=test&link=http://test.com" "http://localhost:3000/posts"
Comments
Post a Comment