我正在尝试创建一个博客网站,但我仍然坚持创建单个“帖子”视图页面。当我从href获取/发布/ id页面时一切正常但当我手动刷新页面或共享链接时,我得到这样的页面:
{"_id":"56fa7443c1542bdc3bdb3857","user":{"_id":"56fa6b0cc83017643c50c5ad",
已经坚持了几天,我该如何解决这个问题?
快递代码(./app/backend/routes/post.js)
router.get('/:id', function (req, res) {
Model.findById(req.params.id, function(err, post){
return res.status(200).send(post);
})
.populate('user');
});
Angular PostCtrl代码(./app/frontend/js/controller/post.js)
$http.get('/post/'+$routeParams.id).success(function (post) {
post.text = $sce.trustAsHtml(post.text);
$scope.post = post;
}).error(function (error, status) {
$scope.errorMessage = error + ' (code:' + status + ')';
});
路线(./app/frontend/js/app.js)
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/post/:id', {
templateUrl: 'views/post.html',
controller: 'PostCtrl'
})
答案 0 :(得分:1)
我修好了。我必须将Angular路由从.when('/blog/:id'
更改为/blog/:id
,并将href更改为/post/:id
。
似乎角度和快速路由不能相同。 Express似乎仍然将数据发送到/blog/:id
,而angular从那里获取数据并立即将其显示给/post/:id
。刷新页面后,一切似乎都正常。只需要立即隐藏/post/:id
中的敏感数据,或找到用户可以访问/**********ASSING A PROTOCOL TO A SOCKET (BIND)************/
if(bind(sock_dest, (struct sockaddr *)&server,sizeof (server))<0)
{
printf("Bind failed\n");
return 1;
}
/**********************************************************/
/*************** LISTEN FOR CONNECTIONS ********************/
listen(sock_dest,6);
printf("Waiting for connections\n");
/**********************************************************/
/****ACCEPT CONNECTION AND MAKE ATHREAD FOR EVERY CLIENT***/
c = sizeof ( struct sockaddr_in);
pthread_t thread_id;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
while( ( client_socket = accept( sock_dest, ( struct sockaddr *)&client, (socklen_t *) &c)) )
{
printf("Connection accepted.\n");
if(client_socket < 0)
{
printf("Accepting connection failed.\n");
return 1;
}
if(pthread_create( &thread_id, &attr, connection_handler, (void*) &client_socket ) < 0)
{
printf("Couldn't create a thread for the new client.\n");
return 1;
}
else
printf("Handler assigned\n");
}
return 0;
}
void *connection_handler (void *socket_dest)
{
char *dest_file_name;
char buffer[300];
int sock = *( int *) socket_dest;
char *exit_signal="EXIT";
if((dest_file_name=create_random_name())==NULL)
{
printf("Generating a random name failed\n");
return NULL;
}
int i=0;
while(1)
{
while(i<299)
{
recv(sock,buffer+i,1,0);
if(buffer[i]=='\n')
{
i=0; // setting i to 0 because next time when we read a path(string) it need to be stored from 0 pozition in array.
break;
}
++i;
}
if((strcmp(buffer,exit_signal))==0)
{
printf("Exit signal received.\n");
return NULL;
}
if((write_line_in_file(buffer,dest_file_name))==1)
{
printf("Failed to write one of the lines in %s\n",dest_file_name);
}
printf("Linie primita:%s\n",buffer);
bzero(buffer,256); // put all bytes to 0 from buffer
}
return 0;
}
char *create_random_name(void)
{
const char charset[] = "abcdefghijklmnopqrstuvwxyz";
char *file_name;
int i=0;
int key;
struct stat stbuf;
time_t t;
while(1)
{
srand((unsigned) time(&t));
if((file_name = malloc(16 * sizeof ( char )) ) == NULL)
{
printf("Failed to alloc memory space\n");
return NULL;
}
strcpy(file_name,"__ft_");
for(i=5 ; i<11 ; i++)
{
key = rand() % (int)(sizeof(charset)-1);
file_name[i]=charset[key];
}
strcat(file_name,".txt");
file_name[15] = '\0';
if(stat(file_name,&stbuf)==-1)
{
break;
}
}
return file_name;
}
/************* RETURN 0 IF SUCCES AND 1 IF FAILS ***********/
int write_line_in_file(char *line,char *file_name)
{
FILE *file;
if((file=fopen(file_name, "a")) == NULL)
{
printf("Can't open %s.\n");
return 1;
}
fprintf(file,"%s",line);
fclose(file);
return 0;
}
的方式。
答案 1 :(得分:0)
我正在尝试理解您的代码,这就是我能想到的
// this is how the route should be using populate
router.get('/post/:id', function (req, res) {
Model.findOne({_id: req.params.id}).populate('user').exec(function (error, post) {
if (post) {
res.status(200).json(post);
} else {
// handle error here...
}
})
});
其他一切看起来还不错。
我希望这会有所帮助。